Association involving multiple tables
I have a data structure simi开发者_如何转开发lar to the following picture.
Table A has_many Table C through Table B; and
Table C has_many Table E through Table D
In order to make Table A has an association to Table E so that I can do a object_a.has_many_object_e
, and having to meet the restrictions below:
- Adding foreign keys to any table is not allowed
- Relationship needs to be bi-directional
What can I do to implement this?
Thanks !!
alt text http://img25.imageshack.us/img25/763/railsassociation.gif
You can use the nested_has_many_through
plugin which will allow you to do:
class ModelA
has_many :model_bs
has_many :model_cs, :through => :modelbs
has_many :model_es, :through => :modelcs
end
class ModelB
belongs_to :model_as
belongs_to :model_cs
end
class ModelC
has_many :model_bs
has_many :model_ds
has_many :model_as, :through => :modelbs
has_many :model_es, :through => :modelcs
end
etc.
精彩评论