views:

28

answers:

1

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 the 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.

A: 

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.

Rob Di Marco
I was a little unclear in my question. I am referring to using an activerecord model to represent the join table, definitely not directly accessing the table.
Chris Johnston
This makes a lot of sense because the PersonGroup is acting not just as a join table, but as an object with state, in this case the membership_state. It definitely makes sense to be its own object and not just a named scope.
Rob Di Marco