From: Brian Candler Date: 2010-08-03T23:58:38+09:00 Subject: Re: Complex associations Trish wrote: > Is there a way to do this? Named or anonymous scopes perhaps. See these posts: http://blog.ethanvizitei.com/2009/05/joins-and-namedscopes-in-activerecord.html http://webjazz.blogspot.com/2008/06/anonymous-scope-unknown-cousin-of-named.html So I'm guessing something like this (completely untested): class Store def products Product.scoped(:joins => :department, :conditions=>["products.store_id=? or (products.department_id = departments.id and departments.store_id=?)", id, id]) end end And the reverse: class Product def store if store_id Store.find(store_id) elsif department_id Store.find_first(:joins => :department, :conditions=>["stores.id = departments.store_id and departments.id = ?", department_id]) end end end I'm not quite sure I've understood the model though. A particular product can only be sold in one department or one store? If another store wants to stock it as well, do you need another Product row? -- Posted via http://www.ruby-forum.com/.