Sorry if this was confusing – I’m still trying to figure out the best
way to explain some of these database setups.
I have an association between tables ‘projects’ and ‘members’. This
association is in a table called ‘projects_members’. The DB would look
like: (this is a simplified, made up database)
state_id --> Points to another table called ‘states’
According to the documentation, when the HABTM relationship is setup
between ‘projects’ and ‘members’, the ‘some_other_column’ is supposed to
magically appear as an attribute that I can read out. The HABTM doc
calls these “additional fields added to the join table”.
This works just fine except when I use the ‘:include’ option to
eager-load ‘states’. When I use :include, the “additional fields” get
A workaround is to define a model for the join table, but this seems to
be something that should work anyhow. I’m also curious – how often do
folks use HABTM rather than just define a model for the join table.
Seems there’s always something in the join that makes it just a little
outside the realm of HABTM.
I think it’s a little unclear what you’re trying to do. find(:include
=> …) specifies table joins. For that to work, you would have to
have an association already defined… example would be:
class Member < ActiveRecord::Base
class State < ActiveRecord::Base
If that is the case, by default you would have a
state_id' field in Member (not astate’ field) as a FK to the states table.
Then, :include => [ :state ] should include that association.
If you’re doing what it looks like you’re trying to do (assign an
enumerated value, State, to each member), you might want to look at
the ActiveRecord Enumerations plugin (http://wiki.rubyonrails.com/