Rails has_many: through conditions and building associations

I'm having trouble creating an association, which is has_many :through with conditions. I have this model:

 class Contact < AR has_many :group_contacts has_many :groups, :through => :group_contacts, :conditions => {:groups => {:published => true}} end 
Problem

occurs when I try to create an instance of a group from a contact. With the syntax above, I get an error:

 contact.groups.build => ActiveRecord::UnknownAttributeError: unknown attribute: groups 

But when I use the following syntax, it works:

 has_many :groups, :through => :group_contacts, :conditions => ['groups.published = ?', true] contact.groups.build => #<Group id: nil, name: nil, description: nil, created_at: nil, updated_at: nil, published: true> 

I see a link to the exact problem in this question . They say that a ticket will be served for this error (in versions 3 versions). I can not find anything on rails 3.0.x

I am using 3.0.8. Has anyone else found this problem?

Additional notes:

I also found that when I build groups, he actually ignores my association conditions when creating. The only reason my above selection had published => true is because it is by default in db.

This is like regression, can anyone check this out?

+7
source share
1 answer
 has_many :groups, :through => :group_contacts, :conditions => {:published => true} 

or

 has_many :groups, :through => :group_contacts, :conditions => {"groups.published" => true} 
+9
source

All Articles