rails named_scope issue with eager loading
        Posted  
        
            by Craig
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Craig
        
        
        
        Published on 2010-06-12T14:30:48Z
        Indexed on 
            2010/06/12
            14:32 UTC
        
        
        Read the original article
        Hit count: 332
        
ruby-on-rails
|named-scope
Two models (Rails 2.3.8):
User; username & disabled properties; User has_one :profile Profile; full_name & hidden properties
I am trying to create a named_scope that eliminate the disabled=1 and hidden=1 User-Profiles. Moreover, while the User model is usually used in conjunction with the Profile model, I would like the flexibility to be able specify this using the :include => :profile syntax.
I have the following User named_scope:
  named_scope :visible, {
    :joins => "INNER JOIN profiles ON users.id=profiles.user_id",
    :conditions => ["users.disabled = ? AND profiles.hidden = ?", false, false]
  }
This works as expected when just reference the User model:
>> User.visible.map(&:username).flatten
=> ["user a", "user b", "user c", "user d"]
However, when I attempt to include the Profile model:
User.visible(:include=> :profiles).profile.map(&:full_name).flatten
I get an error that reads:
NoMethodError: undefined method `profile' for #<User:0x1030bc828>
Am I able to cross model-collection boundaries in this manner?
© Stack Overflow or respective owner