开发者

Why do I get duplicate results using scope on Heroku, but not on my local machine?

开发者 https://www.devze.com 2023-02-09 08:34 出处:网络
I have this scope in Subscriber.rb: scope :notify_today, joins(:skills => :positions). where(\'positions.created_at > subscribers.created_at AND positions.created_at > ?\', 1.day.ago)

I have this scope in Subscriber.rb:

scope :notify_today, 
joins(:skills => :positions).
where('positions.created_at > subscribers.created_at AND positions.created_at > ?', 1.day.ago)

On my local machine it works fine, but on Heroku it returns each object twice.

I managed to fix it with a g开发者_StackOverflow中文版roup:

scope :notify_today, 
joins(:skills => :positions).
where('positions.created_at > subscribers.created_at AND positions.created_at > ?', 1.day.ago).
group('subscribers.email, subscribers.id, subscribers.last_email_at, subscribers.created_at, subscribers.updated_at')

I just wondered if this is the right way to fix it and why Heroku duplicates the results?

EDIT:

Database associations:

Subscriber
has_and_belongs_to_many :skills

Skill
has_and_belongs_to_many :subscribers
has_many :positions

Position
belongs_to :skill

I'm using the default database that comes with Rails.

Subscriber.notify_today.to_sql gives the same thing on my local machine and on heroku:

   "SELECT \"subscribers\".* 
    FROM \"subscribers\" 
    INNER JOIN \"skills_subscribers\" ON \"skills_subscribers\".\"subscriber_id\" = \"subscribers\".\"id\" 
    INNER JOIN \"skills\" ON \"skills\".\"id\" = \"skills_subscribers\".\"skill_id\" 
    INNER JOIN \"positions\" ON \"positions\".\"skill_id\" = \"skills\".\"id\" 
    WHERE (positions.created_at > subscribers.created_at AND positions.created_at > '2011-02-07 10:15:27.098261')"


Tis weird that you're getting different results locally. Just at a guess, Heroku uses a postgresql database by default even if you specify something else in your database.yml I believe.

It might be that your local database understands your SQL differently to postgresql or is joining tables differently..

What database are you using on your local machine?


Need to see the associations between Subscriber, Skill, and Position.

Did you really mean to do join with nested associations, or did you mean to do a join of Subscriber with multiple assocations, joins(:skills, :positions).

0

精彩评论

暂无评论...
验证码 换一张
取 消