Forum: Ruby on Rails HABTM loses 'additional fields' when using :include in find

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E51c6ae07e72cf04ef869868cb8eca6e?d=identicon&s=25 Jake Janovetz (Guest)
on 2005-11-15 07:50
Hi folks-

I've come across a snag that may be a bug, but I'm not sure...

When I do the following (example)--
   @members = @project.members.find(:all)

I get my @members array and it also has additional fields from the table
"parts_members".  Great.

However, when I do:
   @members = @project.members.find(:all,
                                    :include => [ :state ])

where 'state' is a field in the 'members' table, for some reason those
additional fields get clobbered.  Am I doing something wrong?

   Jake
E51c6ae07e72cf04ef869868cb8eca6e?d=identicon&s=25 Guest (Guest)
on 2005-11-25 01:46
I don't normally like to bump my own messages, but I'm still having this
problem.  Has anyone else discovered it?

   Jake


Jake Janovetz wrote:
> Hi folks-
>
> I've come across a snag that may be a bug, but I'm not sure...
>
> When I do the following (example)--
>    @members = @project.members.find(:all)
>
> I get my @members array and it also has additional fields from the table
> "parts_members".  Great.
>
> However, when I do:
>    @members = @project.members.find(:all,
>                                     :include => [ :state ])
>
> where 'state' is a field in the 'members' table, for some reason those
> additional fields get clobbered.  Am I doing something wrong?
>
>    Jake
334805ec86cb3fe1ac2eda776a921fb2?d=identicon&s=25 brad (Guest)
on 2005-11-25 16:18
(Received via mailing list)
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
   belongs_to :state
end

class State < ActiveRecord::Base
   has_many :members
end

If that is the case, by default you would have a `state_id' field in
Member (not a `state' 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/
rails/pages/Plugins).
--
Brad Ediger
866-EDIGERS
E51c6ae07e72cf04ef869868cb8eca6e?d=identicon&s=25 Jake Janovetz <jakejanovetz@yahoo.com> (Guest)
on 2005-11-26 00:38
Hi Brad-

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)

projects
--------
id
name

members
-------
id
name
state_id    --> Points to another table called 'states'

projects_members
----------------
project_id
member_id
some_other_column


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

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.

   Jake


brad wrote:
> 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
>    belongs_to :state
> end
>
> class State < ActiveRecord::Base
>    has_many :members
> end
>
> If that is the case, by default you would have a `state_id' field in
> Member (not a `state' 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/
> rails/pages/Plugins).
> --
> Brad Ediger
> 866-EDIGERS
334805ec86cb3fe1ac2eda776a921fb2?d=identicon&s=25 brad (Guest)
on 2005-11-26 04:49
(Received via mailing list)
I haven't really used HABTM much precisely for this reason...
whenever the join-table information grows larger than just left_id
and right_id, I've usually refactored it into a separate model. There
is a sidebar from DHH in AWDWR about this, and it seems most people
generally use separate models for this situation. (I actually can't
recall any situation in my Rails experience where a HABTM
relationship has survived into production code without being replaced
by a separate model.)

As to why the eager loading clobbers the additional fields, I'm not
sure. I would suggest taking a look at the SQL queries being
generated for the find()... maybe it's joining the tables differently
than you would expect.

Good luck--
be


On Nov 25, 2005, at 5:38 PM, Jake Janovetz <jakejanovetz@yahoo.com>
This topic is locked and can not be replied to.