Unknown
2005-Nov-15 06:50 UTC
HABTM loses ''additional fields'' when using :include in find
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 -- Posted via http://www.ruby-forum.com/.
jakejanovetz-/E1597aS9LQAvxtiuMwx3w@public.gmane.org
2005-Nov-25 00:46 UTC
Re: HABTM loses ''additional fields'' when using :include in f
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-- Posted via http://www.ruby-forum.com/.
Brad Ediger
2005-Nov-25 15:14 UTC
Re: Re: HABTM loses ''additional fields'' when using :include in f
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 On Nov 24, 2005, at 6:46 PM, jakejanovetz-/E1597aS9LQAvxtiuMwx3w@public.gmane.org wrote:> 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 > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Jake Janovetz
2005-Nov-25 23:38 UTC
Re: Re: HABTM loses ''additional fields'' when using :include
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-- Posted via http://www.ruby-forum.com/.
Brad Ediger
2005-Nov-26 03:47 UTC
Re: Re: Re: HABTM loses ''additional fields'' when using :include
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-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote:> 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 > >