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 | class Choice < ActiveRecord::Base | 1 | class Choice < ActiveRecord::Base |
| 2 | acts_as_versioned :if_changed => [:data, :creator_id, :question_id, :active] | 2 | acts_as_versioned :if_changed => [:data, :creator_id, :question_id, :active] |
| 3 | - | 3 | + |
| 4 | belongs_to :question, :counter_cache => true | 4 | belongs_to :question, :counter_cache => true |
| 5 | belongs_to :creator, :class_name => "Visitor", :foreign_key => "creator_id" | 5 | belongs_to :creator, :class_name => "Visitor", :foreign_key => "creator_id" |
| 6 | - | 6 | + |
| 7 | validates_presence_of :creator, :on => :create, :message => "can't be blank" | 7 | validates_presence_of :creator, :on => :create, :message => "can't be blank" |
| 8 | validates_presence_of :question, :on => :create, :message => "can't be blank" | 8 | validates_presence_of :question, :on => :create, :message => "can't be blank" |
| 9 | validates_presence_of :data | 9 | validates_presence_of :data |
| 10 | #validates_length_of :item, :maximum => 140 | 10 | #validates_length_of :item, :maximum => 140 |
| 11 | - | 11 | + |
| 12 | has_many :votes | 12 | has_many :votes |
| 13 | has_many :losing_votes, :class_name => "Vote", :foreign_key => "loser_choice_id" | 13 | has_many :losing_votes, :class_name => "Vote", :foreign_key => "loser_choice_id" |
| 14 | has_many :flags | 14 | has_many :flags |
| @@ -25,7 +25,7 @@ class Choice < ActiveRecord::Base | @@ -25,7 +25,7 @@ class Choice < ActiveRecord::Base | ||
| 25 | named_scope :not_created_by, lambda { |creator_id| | 25 | named_scope :not_created_by, lambda { |creator_id| |
| 26 | { :conditions => ["creator_id <> ?", creator_id] } | 26 | { :conditions => ["creator_id <> ?", creator_id] } |
| 27 | } | 27 | } |
| 28 | - | 28 | + |
| 29 | after_save :update_questions_counter | 29 | after_save :update_questions_counter |
| 30 | after_save :update_prompt_queue | 30 | after_save :update_prompt_queue |
| 31 | 31 | ||
| @@ -37,7 +37,7 @@ class Choice < ActiveRecord::Base | @@ -37,7 +37,7 @@ class Choice < ActiveRecord::Base | ||
| 37 | unless part_of_batch_create | 37 | unless part_of_batch_create |
| 38 | self.question.update_attribute(:inactive_choices_count, self.question.choices.inactive.length) | 38 | self.question.update_attribute(:inactive_choices_count, self.question.choices.inactive.length) |
| 39 | end | 39 | end |
| 40 | - end | 40 | + end |
| 41 | 41 | ||
| 42 | # if changing a choice to active, we want to regenerate prompts | 42 | # if changing a choice to active, we want to regenerate prompts |
| 43 | def update_prompt_queue | 43 | def update_prompt_queue |
| @@ -50,7 +50,7 @@ class Choice < ActiveRecord::Base | @@ -50,7 +50,7 @@ class Choice < ActiveRecord::Base | ||
| 50 | end | 50 | end |
| 51 | end | 51 | end |
| 52 | end | 52 | end |
| 53 | - | 53 | + |
| 54 | def before_create | 54 | def before_create |
| 55 | unless self.score | 55 | unless self.score |
| 56 | self.score = 50.0 | 56 | self.score = 50.0 |
| @@ -63,14 +63,19 @@ class Choice < ActiveRecord::Base | @@ -63,14 +63,19 @@ class Choice < ActiveRecord::Base | ||
| 63 | end | 63 | end |
| 64 | return true #so active record will save | 64 | return true #so active record will save |
| 65 | end | 65 | end |
| 66 | - | 66 | + |
| 67 | def compute_score | 67 | def compute_score |
| 68 | (wins.to_f+1)/(wins+1+losses+1) * 100 | 68 | (wins.to_f+1)/(wins+1+losses+1) * 100 |
| 69 | end | 69 | end |
| 70 | - | 70 | + |
| 71 | def compute_score! | 71 | def compute_score! |
| 72 | self.score = compute_score | 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 | end | 79 | end |
| 75 | 80 | ||
| 76 | def user_created | 81 | def user_created |
| @@ -101,15 +106,15 @@ class Choice < ActiveRecord::Base | @@ -101,15 +106,15 @@ class Choice < ActiveRecord::Base | ||
| 101 | (self.active = true) | 106 | (self.active = true) |
| 102 | self.save! | 107 | self.save! |
| 103 | end | 108 | end |
| 104 | - | 109 | + |
| 105 | def deactivate! | 110 | def deactivate! |
| 106 | (self.active = false) | 111 | (self.active = false) |
| 107 | self.save! | 112 | self.save! |
| 108 | end | 113 | end |
| 109 | - | 114 | + |
| 110 | protected | 115 | protected |
| 111 | 116 | ||
| 112 | - | 117 | + |
| 113 | def generate_prompts | 118 | def generate_prompts |
| 114 | #once a choice is added, we need to generate the new prompts (possible combinations of choices) | 119 | #once a choice is added, we need to generate the new prompts (possible combinations of choices) |
| 115 | #do this in a new process (via delayed jobs)? Maybe just for uploaded ideas | 120 | #do this in a new process (via delayed jobs)? Maybe just for uploaded ideas |
| @@ -123,11 +128,23 @@ class Choice < ActiveRecord::Base | @@ -123,11 +128,23 @@ class Choice < ActiveRecord::Base | ||
| 123 | previous_choices.each do |r| | 128 | previous_choices.each do |r| |
| 124 | inserts.push("(NULL, #{self.question_id}, NULL, #{self.id}, '#{timestring}', '#{timestring}', NULL, 0, #{r.id}, NULL, NULL)") | 129 | inserts.push("(NULL, #{self.question_id}, NULL, #{self.id}, '#{timestring}', '#{timestring}', NULL, 0, #{r.id}, NULL, NULL)") |
| 125 | end | 130 | end |
| 126 | - #add prompts with this choice on the right | 131 | + #add prompts with this choice on the right |
| 127 | previous_choices.each do |l| | 132 | previous_choices.each do |l| |
| 128 | inserts.push("(NULL, #{self.question_id}, NULL, #{l.id}, '#{timestring}', '#{timestring}', NULL, 0, #{self.id}, NULL, NULL)") | 133 | inserts.push("(NULL, #{self.question_id}, NULL, #{l.id}, '#{timestring}', '#{timestring}', NULL, 0, #{self.id}, NULL, NULL)") |
| 129 | end | 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 | Question.update_counters(self.question_id, :prompts_count => 2*previous_choices.size) | 149 | Question.update_counters(self.question_id, :prompts_count => 2*previous_choices.size) |
| 133 | 150 | ||
| @@ -142,3 +159,4 @@ class Choice < ActiveRecord::Base | @@ -142,3 +159,4 @@ class Choice < ActiveRecord::Base | ||
| 142 | #} | 159 | #} |
| 143 | end | 160 | end |
| 144 | end | 161 | end |
| 162 | + |