Rails Named Scope and overlapping conditions

Posted by Tumtu on Stack Overflow See other posts from Stack Overflow or by Tumtu
Published on 2010-04-14T08:53:48Z Indexed on 2010/05/27 12:01 UTC
Read the original article Hit count: 221

Hi everyone, have a question about rails SQL generation:

class Organization < ActiveRecord::Base  
  has_many :people
  named_scope :active, :conditions => { :active => 'Yes' }
end

class Person < ActiveRecord::Base  
   belongs_to :organization
end

Rails SQL for all active people in the first organiztion

Organization.first.people.active.all
[4;36;1mOrganization Load (0.0ms)[0m [0;1mSELECT TOP 1 * FROM [organizations] [0m
[4;35;1mPerson Load (0.0ms)[0m [0mSELECT * FROM [people] WHERE ((([people].[active] = 'Yes') AND ([people].organization_id = 1)) AND ([people].organization_id = 1)) [0m

Why Rails generates "[people].organization_id = 1" condition twice ? Does someone know how to make it DRY ?

e.g.

SELECT * FROM [people] WHERE (([people].[active] = 'Yes') AND ([people].organization_id = 1))

© Stack Overflow or respective owner

Related posts about sql

Related posts about ruby-on-rails