class Store < ActiveRecord::Base
has_many: store_type
has_many: product_type, :through => department

class Department < ActiveRecord::Base
belongs_to: store
belongs_to: product

class Product < ActiveRecord::Base
  has_many :departments
  has_many :stores, :through => department
end

I think you need to remove the StoreType as a model...

On Mon, Aug 2, 2010 at 11:05 AM, Trish <[email protected]> wrote:

> I am working on a project that has some complex table associations,
> and I am having a hard time declaring this association in a Model, and
> fear it can't be done.
>
> Here is an example of my issue...
>
> class StoreType < ActiveRecord::Base; end
> class Store < ActiveRecord::Base; end
> class Department < ActiveRecord::Base; end
> class Product < ActiveRecord::Base; end
>
> A StoreType has many stores and a Store has many Department.  However,
> both Store and Department have many products.  An product has the
> columns store_id and department_id which are mutually exclusive.  This
> is because a Product may belong directly to a Department, or it may be
> a 'global' product that belongs directly to the Store.
>
> What I'd like to do is have an association in the StoreType model that
> would give me all products for that StoreType.
>
> Currently, I have set up the following associtations on StoreType:
>
> class StoreType < ActiveRecord::Base
>  has_many :stores
>  has_many :departments, :through=>:stores
>
> has_many :store_products, :through=>:stores, :source=>:products, :uniq
> => true
>
> has_many :department_products, :through=>:departments, :source=>:products,
> :uniq
> => true
> end
>
> This is using the Nested Has Many Through plugin to achieve the nested
> association (department_products).
>
> However, I'd like to have a generic 'products' association for
> StoreType that pulls a combination of the two product associations.
> I'd like to do this through an association instead of just through a
> function because I want to gain the dynamic methods created by
> has_many, specifically the "collection.find(...)" method so I can add
> more conditions to the collection.
>
> Is there a way to do this?
>
> Thanks in advance!
> Trish
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<rubyonrails-talk%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to