rails/rails — Issue #5178: Creating has_many :through association with conditions
Author: heaven
Created: 2012-02-25 22:07:24+00:00
State: open

Hi, I have troubles when trying to use such code:

````ruby
class Site < ActiveRecord::Base
has_many :site_relationships, :dependent => :destroy
has_many :profiles, :through => :site_relationships
end

class SiteRelationship < ActiveRecord::Base
VALID_KINDS = {:owner => "Owner", :reader => "Reader"}

belongs_to :profile
belongs_to :site

validates :kind, :inclusion => VALID_KINDS.values, :allow_nil => true
end

class Profile < ActiveRecord::Base
has_many :site_relationships, :dependent => :destroy
has_many :owned_sites, :through => :site_relationships, :source => :site, :conditions => "site_relationships.kind = 'Owned'"
end
````

Actually profile.owned_sites works great, but when I try to

````ruby
profile.owned_sites.create(:url => "http://google.com")
````

It creates the Site and creates the SiteRelationship but doesn't set the site_relationships.kind attribute to 'Owned', so: profile.owned_sites.reload => []

Is there any way to set up the join model attribute automatically? Or other way to solve this issue?

Thanks in advance,
Alex