Ruby on Rails Friday, July 10, 2015

Cant undestand what wrong with syntax sql file migration. please, help:

i run rake:db:migrate

error:

Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1: UPDATE photos SET votes_count = 0, rating = 0 WHERE id NOT IN ()/home/user/myapp/appmame/db/migrate/20131110162613_recalculate_photos_rating.rb:8:in `up'


class RecalculatePhotosRating < ActiveRecord::Migration
 
def up
   
if Vote.where("subject_type='Photo' and rating > 1").count > 0
     
ActiveRecord::Base.connection.execute "DELETE FROM votes WHERE subject_type='Photo' AND rating <= 5"
     
ActiveRecord::Base.connection.execute "UPDATE votes SET rating=1 WHERE subject_type='Photo'"
   
end
    pids
= Vote.where(subject_type: 'Photo').pluck(:subject_id).uniq
   
ActiveRecord::Base.connection.execute "UPDATE photos SET votes_count = 0, rating = 0 WHERE id NOT IN (#{pids.join(', ')})"
   
ActiveRecord::Base.connection.execute "UPDATE photos SET rating=(SELECT COUNT(*) FROM votes WHERE votes.subject_type='Photo' AND subject_id=photos.id) WHERE id IN (#{pids.join(', ')})"
   
ActiveRecord::Base.connection.execute "UPDATE photos SET votes_count=(SELECT COUNT(*) FROM votes WHERE votes.subject_type='Photo' AND subject_id=photos.id) WHERE id IN (#{pids.join(', ')})"
 
end

 
def down
 
end
end



--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/8da9a72e-df1c-440a-9033-358987bd9863%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment