开发者

Any good reason to access many-to-many join table directly?

I have a Ruby on Rails application in which the code directly access many-to-many join tables directly. This makes modifying some of the code very difficult since it bypasses 开发者_如何学Cthe normal associations and methods created via the :has_many and :has_many :through relationships.

My question is simply, is this an acceptable thing to be doing or should this be avoided?

In my mind, from the database point of view, the join tables do not exist in the logical view. As such, there should be no reason to ever access them directly. One should simply ignore that they even exist and let the framework take care of them.

What do others think?

To be more precise, I am wondering if the following should ever appear in code:

Person { name }
Group { name }
PersonGroup { person_id, group_id, membership_state }
PersonGroup.find(:all, :conditions => {:membership_state => 'pending'} }

I am thinking that instead of using the join table, one should instead scope the request to the Group and find all pending members that way.


I would go with "should be avoided". If you really need to manipulate the data in the join table, I would suggest creating an ActiveRecord object that maps to that table and then manipulating through that object. In general, I have found direct data manipulation when using an Object/Relational framework (not just ActiveRecord) leads to complications.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜