I am trying to use one join model for two separate but very similar associations. Here is what I have:
Two primary models: Package, Size
Pacakges have many sizes but there’s a wrinkle. The sizes need to be allocated as a size for top or bottom. My current associations on Package are:
has_many :package_sizes
has_many :sizes, :through => :package_sizes
has_many :bottoms_sizes, :through => :package_sizes, :scope => {:package_sizes => {:bodylocation => "B"}}, :source => :size
has_many :tops_sizes, :through => :package_sizes, :scope => {:package_sizes => {:bodylocation => "T"}}, :source => :size
PackageSize is a join model with: size_id | package_id | bodylocation:string
I have a failing test to verify it is working:
@p = Package.new
@size1 = Size.first
@p.tops_sizes << @size1
@p.save
@p.reload
@p.tops_sizes.should include(@size1)
This should work properly but for some reason the bodylocation field does not get automatically set.
Any ideas?
Try creating two separate through associations for this.