Commit 805e054f040cc30c67360a69954fc866f406f0bd
1 parent
55b3c08c
Exists in
master
and in
1 other branch
fixed sql issues in postgres database
Showing
1 changed file
with
32 additions
and
14 deletions
Show diff stats
app/models/choice.rb
| 1 | 1 | class Choice < ActiveRecord::Base |
| 2 | 2 | acts_as_versioned :if_changed => [:data, :creator_id, :question_id, :active] |
| 3 | - | |
| 3 | + | |
| 4 | 4 | belongs_to :question, :counter_cache => true |
| 5 | 5 | belongs_to :creator, :class_name => "Visitor", :foreign_key => "creator_id" |
| 6 | - | |
| 6 | + | |
| 7 | 7 | validates_presence_of :creator, :on => :create, :message => "can't be blank" |
| 8 | 8 | validates_presence_of :question, :on => :create, :message => "can't be blank" |
| 9 | 9 | validates_presence_of :data |
| 10 | 10 | #validates_length_of :item, :maximum => 140 |
| 11 | - | |
| 11 | + | |
| 12 | 12 | has_many :votes |
| 13 | 13 | has_many :losing_votes, :class_name => "Vote", :foreign_key => "loser_choice_id" |
| 14 | 14 | has_many :flags |
| ... | ... | @@ -25,7 +25,7 @@ class Choice < ActiveRecord::Base |
| 25 | 25 | named_scope :not_created_by, lambda { |creator_id| |
| 26 | 26 | { :conditions => ["creator_id <> ?", creator_id] } |
| 27 | 27 | } |
| 28 | - | |
| 28 | + | |
| 29 | 29 | after_save :update_questions_counter |
| 30 | 30 | after_save :update_prompt_queue |
| 31 | 31 | |
| ... | ... | @@ -37,7 +37,7 @@ class Choice < ActiveRecord::Base |
| 37 | 37 | unless part_of_batch_create |
| 38 | 38 | self.question.update_attribute(:inactive_choices_count, self.question.choices.inactive.length) |
| 39 | 39 | end |
| 40 | - end | |
| 40 | + end | |
| 41 | 41 | |
| 42 | 42 | # if changing a choice to active, we want to regenerate prompts |
| 43 | 43 | def update_prompt_queue |
| ... | ... | @@ -50,7 +50,7 @@ class Choice < ActiveRecord::Base |
| 50 | 50 | end |
| 51 | 51 | end |
| 52 | 52 | end |
| 53 | - | |
| 53 | + | |
| 54 | 54 | def before_create |
| 55 | 55 | unless self.score |
| 56 | 56 | self.score = 50.0 |
| ... | ... | @@ -63,14 +63,19 @@ class Choice < ActiveRecord::Base |
| 63 | 63 | end |
| 64 | 64 | return true #so active record will save |
| 65 | 65 | end |
| 66 | - | |
| 66 | + | |
| 67 | 67 | def compute_score |
| 68 | 68 | (wins.to_f+1)/(wins+1+losses+1) * 100 |
| 69 | 69 | end |
| 70 | - | |
| 70 | + | |
| 71 | 71 | def compute_score! |
| 72 | 72 | self.score = compute_score |
| 73 | - Choice.connection.execute("UPDATE `choices` SET `score` = #{self.score}, `updated_at` = '#{Time.now.utc.to_s(:db)}' WHERE `id` = #{self.id}") | |
| 73 | + #changed to quote tables according the database type (wasn't working in postgres) | |
| 74 | + Choice.connection.execute("UPDATE #{connection.quote_table_name("choices")} | |
| 75 | + SET #{connection.quote_column_name("score")} = #{self.score}, | |
| 76 | + #{connection.quote_column_name("updated_at")} = '#{Time.now.utc.to_s(:db)}' | |
| 77 | + WHERE | |
| 78 | + #{connection.quote_column_name("id")} = #{self.id}") | |
| 74 | 79 | end |
| 75 | 80 | |
| 76 | 81 | def user_created |
| ... | ... | @@ -101,15 +106,15 @@ class Choice < ActiveRecord::Base |
| 101 | 106 | (self.active = true) |
| 102 | 107 | self.save! |
| 103 | 108 | end |
| 104 | - | |
| 109 | + | |
| 105 | 110 | def deactivate! |
| 106 | 111 | (self.active = false) |
| 107 | 112 | self.save! |
| 108 | 113 | end |
| 109 | - | |
| 114 | + | |
| 110 | 115 | protected |
| 111 | 116 | |
| 112 | - | |
| 117 | + | |
| 113 | 118 | def generate_prompts |
| 114 | 119 | #once a choice is added, we need to generate the new prompts (possible combinations of choices) |
| 115 | 120 | #do this in a new process (via delayed jobs)? Maybe just for uploaded ideas |
| ... | ... | @@ -123,11 +128,23 @@ class Choice < ActiveRecord::Base |
| 123 | 128 | previous_choices.each do |r| |
| 124 | 129 | inserts.push("(NULL, #{self.question_id}, NULL, #{self.id}, '#{timestring}', '#{timestring}', NULL, 0, #{r.id}, NULL, NULL)") |
| 125 | 130 | end |
| 126 | - #add prompts with this choice on the right | |
| 131 | + #add prompts with this choice on the right | |
| 127 | 132 | previous_choices.each do |l| |
| 128 | 133 | inserts.push("(NULL, #{self.question_id}, NULL, #{l.id}, '#{timestring}', '#{timestring}', NULL, 0, #{self.id}, NULL, NULL)") |
| 129 | 134 | end |
| 130 | - sql = "INSERT INTO `prompts` (`algorithm_id`, `question_id`, `voter_id`, `left_choice_id`, `created_at`, `updated_at`, `tracking`, `votes_count`, `right_choice_id`, `active`, `randomkey`) VALUES #{inserts.join(', ')}" | |
| 135 | + #changed to quote tables according the database type (wasn't working in postgres) | |
| 136 | + sql = "INSERT INTO #{connection.quote_table_name("prompts")} | |
| 137 | + (#{connection.quote_column_name("algorithm_id")}, | |
| 138 | + #{connection.quote_column_name("question_id")}, | |
| 139 | + #{connection.quote_column_name("voter_id")}, | |
| 140 | + #{connection.quote_column_name("left_choice_id")}, | |
| 141 | + #{connection.quote_column_name("created_at")}, | |
| 142 | + #{connection.quote_column_name("updated_at")}, | |
| 143 | + #{connection.quote_column_name("tracking")}, | |
| 144 | + #{connection.quote_column_name("votes_count")}, | |
| 145 | + #{connection.quote_column_name("right_choice_id")}, | |
| 146 | + #{connection.quote_column_name("active")}, | |
| 147 | + #{connection.quote_column_name("randomkey")}) VALUES #{inserts.join(', ')}" | |
| 131 | 148 | |
| 132 | 149 | Question.update_counters(self.question_id, :prompts_count => 2*previous_choices.size) |
| 133 | 150 | |
| ... | ... | @@ -142,3 +159,4 @@ class Choice < ActiveRecord::Base |
| 142 | 159 | #} |
| 143 | 160 | end |
| 144 | 161 | end |
| 162 | + | ... | ... |