I''ve got the following setup tables: order_items ----- id name barcode qty audit_items ----- id name barcode locations ----- id name item_locations ----- barcode location_id num_requests models: OrderItemController < ActiveRecord::Base has_and_belongs_to_many :locations, :join_table => :item_locations, :foreign_key => :barcode end AudittemController < ActiveRecord::Base has_and_belongs_to_many :locations, :join_table => :item_locations, :foreign_key => :barcode end LocationController < ActiveRecord::Base has_and_belongs_to_many :order_items, :join_table => :item_locations has_and_belongs_to_many :audit_items, :join_table => :item_locations end the purpose of the barcodes column in the item tables is to uniquely identify a box. that box might hold multiple items and the order item record will indicate how many items to pick out of the box for that item (qty field). the locations table is a lookup table of possible locations where a box could be located (shelf, order dropoff table, shipping table, etc) the item_locations table is used to track where a particular box is currently located. when an order item or audit item record is added, a check is done to see if the barcode exists in the item_locations table. if it does, num_requests is incremented. if it does not, a record is added to item_locations to indicated the current location. when an item is removed from the order_item or audit_item table, the num_requests is decremented, or removed if no more requests exist (0). multiple orders records can reference the same barcode. IE, bill and bob can both order 500 qty of part xyz, which is in box 123456. two separate order item entries. this would result in a entry in item_locations as 123456, 1, 2), 1 being the location.id. so that being said, when i use habtm relationship, AR wants to use the (order/audit) item.id as the key for item_locations, when i need to use the (order/audit) item.barcode to lookup the location in item_locations i''ve also tried specifying :association_foreign_key => :barcode, but that has no effect this is the AR generated SQL...you can see where it uses the id column where i want it to use the barcode column. (surrounded by ***) SELECT locations.id AS t1_r0, locations.name AS t1_r1, order_items.id AS t0_r0, order_items.name AS t0_r1, order_items.barcode AS t0_r2 FROM order_items LEFT OUTER JOIN item_locations ON *** item_locations.barcode = order_items.id *** LEFT OUTER JOIN locations ON item_locations.barcode = locations.id i need that to read "item_locations.barcode = order_item.barcode" any ideas on how i can do this? Chris
Chris, Take a look at the finder_sql option for the habtm relationship. Cody On 10/17/05, Chris Hall <christopher.k.hall-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''ve got the following setup > > tables: > > order_items > ----- > id > name > barcode > qty > > audit_items > ----- > id > name > barcode > > locations > ----- > id > name > > item_locations > ----- > barcode > location_id > num_requests > > models: > > OrderItemController < ActiveRecord::Base > has_and_belongs_to_many :locations, :join_table => :item_locations, > :foreign_key => :barcode > end > > AudittemController < ActiveRecord::Base > has_and_belongs_to_many :locations, :join_table => :item_locations, > :foreign_key => :barcode > end > > LocationController < ActiveRecord::Base > has_and_belongs_to_many :order_items, :join_table => :item_locations > has_and_belongs_to_many :audit_items, :join_table => :item_locations > end > > the purpose of the barcodes column in the item tables is to uniquely > identify a box. that box might hold multiple items and the order item > record will indicate how many items to pick out of the box for that > item (qty field). > > the locations table is a lookup table of possible locations where a > box could be located (shelf, order dropoff table, shipping table, etc) > > the item_locations table is used to track where a particular box is > currently located. when an order item or audit item record is added, > a check is done to see if the barcode exists in the item_locations > table. if it does, num_requests is incremented. if it does not, a > record is added to item_locations to indicated the current location. > when an item is removed from the order_item or audit_item table, the > num_requests is decremented, or removed if no more requests exist (0). > > multiple orders records can reference the same barcode. IE, bill and > bob can both order 500 qty of part xyz, which is in box 123456. two > separate order item entries. this would result in a entry in > item_locations as 123456, 1, 2), 1 being the location.id. > > so that being said, when i use habtm relationship, AR wants to use the > (order/audit) item.id as the key for item_locations, when i need to > use the (order/audit) item.barcode to lookup the location in > item_locations > > i''ve also tried specifying :association_foreign_key => :barcode, but > that has no effect > > this is the AR generated SQL...you can see where it uses the id column > where i want it to use the barcode column. (surrounded by ***) > > SELECT locations.id AS t1_r0, locations.name AS t1_r1, order_items.id > AS t0_r0, order_items.name AS t0_r1, order_items.barcode AS t0_r2 FROM > order_items LEFT OUTER JOIN item_locations ON *** > item_locations.barcode = order_items.id *** LEFT OUTER JOIN locations > ON item_locations.barcode = locations.id > > i need that to read "item_locations.barcode = order_item.barcode" > > any ideas on how i can do this? > > Chris > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
ok, this defintely looks like the route i need to take...but i''ve got a problem now... using the following query string (going by an example from the rails api docs): has_many :locations, :finder_sql => "SELECT locations.* " + "FROM locations, item_locations " + "where item_locations.barcode ''#{barcode}'' and " + "item_locations.location_id locations.id" i get the following error: undefined local variable or method `barcode'' for OrderItem:Class /app/models/order_item.rb:5 /app/controllers/order_item_controller.rb:4:in `list'' the way i assume the finder sql works is that for each item, the query will get run using the barcode attribute(method) to fill in the blank. but this doesn''t seem to work. On 10/17/05, Cody Fauser <codyfauser-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Chris, > > Take a look at the finder_sql option for the habtm relationship. > > > Cody > > > On 10/17/05, Chris Hall <christopher.k.hall-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > I''ve got the following setup > > > > tables: > > > > order_items > > ----- > > id > > name > > barcode > > qty > > > > audit_items > > ----- > > id > > name > > barcode > > > > locations > > ----- > > id > > name > > > > item_locations > > ----- > > barcode > > location_id > > num_requests > > > > models: > > > > OrderItemController < ActiveRecord::Base > > has_and_belongs_to_many :locations, :join_table => :item_locations, > > :foreign_key => :barcode > > end > > > > AudittemController < ActiveRecord::Base > > has_and_belongs_to_many :locations, :join_table => :item_locations, > > :foreign_key => :barcode > > end > > > > LocationController < ActiveRecord::Base > > has_and_belongs_to_many :order_items, :join_table => :item_locations > > has_and_belongs_to_many :audit_items, :join_table => :item_locations > > end > > > > the purpose of the barcodes column in the item tables is to uniquely > > identify a box. that box might hold multiple items and the order item > > record will indicate how many items to pick out of the box for that > > item (qty field). > > > > the locations table is a lookup table of possible locations where a > > box could be located (shelf, order dropoff table, shipping table, etc) > > > > the item_locations table is used to track where a particular box is > > currently located. when an order item or audit item record is added, > > a check is done to see if the barcode exists in the item_locations > > table. if it does, num_requests is incremented. if it does not, a > > record is added to item_locations to indicated the current location. > > when an item is removed from the order_item or audit_item table, the > > num_requests is decremented, or removed if no more requests exist (0). > > > > multiple orders records can reference the same barcode. IE, bill and > > bob can both order 500 qty of part xyz, which is in box 123456. two > > separate order item entries. this would result in a entry in > > item_locations as 123456, 1, 2), 1 being the location.id. > > > > so that being said, when i use habtm relationship, AR wants to use the > > (order/audit) item.id as the key for item_locations, when i need to > > use the (order/audit) item.barcode to lookup the location in > > item_locations > > > > i''ve also tried specifying :association_foreign_key => :barcode, but > > that has no effect > > > > this is the AR generated SQL...you can see where it uses the id column > > where i want it to use the barcode column. (surrounded by ***) > > > > SELECT locations.id AS t1_r0, locations.name AS t1_r1, order_items.id > > AS t0_r0, order_items.name AS t0_r1, order_items.barcode AS t0_r2 FROM > > order_items LEFT OUTER JOIN item_locations ON *** > > item_locations.barcode = order_items.id *** LEFT OUTER JOIN locations > > ON item_locations.barcode = locations.id > > > > i need that to read "item_locations.barcode = order_item.barcode" > > > > any ideas on how i can do this? > > > > Chris > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Okay.... Well take a quick look at the book again.... You should try this. I''ve simplified it for you by letting Rails do the pluralization (since you''re just starting out, let Rails do the work.... Learn to customize your schema later!) Tables: (note plurality) parties ------ Id parties_roles ---------- party_id - fk to party(id) role_id - fk to party_role_type(id) roles --------------- id name etc... *** Note that your join table is alphabetical, and plural for both models (parties, roles Now on to your classes: class Party < ActiveRecord::Base has_and_belongs_to_many :roles end class Role < ActiveRecord::Base has_and_belongs_to_many :parties end So now, you can do @parties = Role.find(1).parties Or @roles = Party.find(1).roles Hope that helps! -bph -----Original Message----- From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Jason Lee Sent: Friday, October 21, 2005 2:59 PM To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: [Rails] habtm question Hi, I''ve been following the examples in the Agile Web Development w/Ruby book and have run across a habtm issue and would like some input. I have the following three tables: party ------ id party_role ---------- party_id - fk to party(id) party_role_type_id - fk to party_role_type(id) party_role_type --------------- id name etc... On pg 240 of the rails book it describes the following relationship: articles -------- id title articles_users -------------- article_id user_id users ----- id name For all practical purposes, my design above mimics this and is a pretty standard join table. However, when I find() my PartyRole AR object, it doesn''t automagically connect the two object, it only provides the local attributes. The AR objects are as follows: class Party < ActiveRecord::Base set_table_name "party" has_and_belongs_to_many :partyRoleType end class PartyRole < ActiveRecord::Base set_table_name "party_role" end class PartyRoleType < ActiveRecord::Base set_table_name "party_role_type" has_and_belongs_to_many :party end I think my problem here is mostly naming issues and this will probably be an ongoing issue for me during my migration tests. Does anyone know of any way that I can specify what the join elements in this case should be? I''m sure Rails expects ''Roles'' vs. PartyRoleType in my case and this is what I''m looking to override. I''m having fun playing w/Rails, but it''s taking a little ''adjustment'' for some of the AR stuff as my db is pretty much already defined and I can''t rename or change things easily to match Rails naming conventions. I''m looking for the middle ground in other words. That said, I''d still like to keep moving forward and would appreciate any input. Thanks, - jason _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
As a follow-up, since you said that you really can''t change the db, you''ll have to do a little more tweaking to my example.... I think the problem you''re having is that you''ve made a model for your join table. With most HABTM examples, you don''t do that. You''d create an instance of a Party object and tell it to get roles, and Rails attempts to find the bridge table for you. Try this: class Party < ActiveRecord::Base set_table_name "party" has_and_belongs_to_many :partyRoleType end class PartyRoleType < ActiveRecord::Base set_table_name "party_role_type" has_and_belongs_to_many :party end Then look at has_and_belongs_to_many to see how to specify the tables for the join.... I believe it''s has_and_belongs_to_many :party, :join_table=>"PartyRoles" -bph -----Original Message----- From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Hogan, Brian P. Sent: Friday, October 21, 2005 3:18 PM To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: RE: [Rails] habtm question Okay.... Well take a quick look at the book again.... You should try this. I''ve simplified it for you by letting Rails do the pluralization (since you''re just starting out, let Rails do the work.... Learn to customize your schema later!) Tables: (note plurality) parties ------ Id parties_roles ---------- party_id - fk to party(id) role_id - fk to party_role_type(id) roles --------------- id name etc... *** Note that your join table is alphabetical, and plural for both models (parties, roles Now on to your classes: class Party < ActiveRecord::Base has_and_belongs_to_many :roles end class Role < ActiveRecord::Base has_and_belongs_to_many :parties end So now, you can do @parties = Role.find(1).parties Or @roles = Party.find(1).roles Hope that helps! -bph -----Original Message----- From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Jason Lee Sent: Friday, October 21, 2005 2:59 PM To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: [Rails] habtm question Hi, I''ve been following the examples in the Agile Web Development w/Ruby book and have run across a habtm issue and would like some input. I have the following three tables: party ------ id party_role ---------- party_id - fk to party(id) party_role_type_id - fk to party_role_type(id) party_role_type --------------- id name etc... On pg 240 of the rails book it describes the following relationship: articles -------- id title articles_users -------------- article_id user_id users ----- id name For all practical purposes, my design above mimics this and is a pretty standard join table. However, when I find() my PartyRole AR object, it doesn''t automagically connect the two object, it only provides the local attributes. The AR objects are as follows: class Party < ActiveRecord::Base set_table_name "party" has_and_belongs_to_many :partyRoleType end class PartyRole < ActiveRecord::Base set_table_name "party_role" end class PartyRoleType < ActiveRecord::Base set_table_name "party_role_type" has_and_belongs_to_many :party end I think my problem here is mostly naming issues and this will probably be an ongoing issue for me during my migration tests. Does anyone know of any way that I can specify what the join elements in this case should be? I''m sure Rails expects ''Roles'' vs. PartyRoleType in my case and this is what I''m looking to override. I''m having fun playing w/Rails, but it''s taking a little ''adjustment'' for some of the AR stuff as my db is pretty much already defined and I can''t rename or change things easily to match Rails naming conventions. I''m looking for the middle ground in other words. That said, I''d still like to keep moving forward and would appreciate any input. Thanks, - jason _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Brian, Thanks for the reply. I understand completely what you''re saying below and I would love to just be able to have Rails manage everything for me. However, it''s looking like this isn''t possible for me right now (I cannot start re-doing my whole db layer at the moment), so I guess my question is this: If I cannot have Rails manage the pluralization (because of me and not Rails) is there a way I can tell AR what to use instead? Like if I''m using habtm, isn''t there something equiv to ''set_table_name "my_table"'' which let''s me specify the actual join table name and overrides the Rails pluralization? Thanks again, - jason On Oct 21, 2005, at 1:18 PM, Hogan, Brian P. wrote:> > Okay.... Well take a quick look at the book again.... > > You should try this. I''ve simplified it for you by letting Rails do > the > pluralization (since you''re just starting out, let Rails do the > work.... > Learn to customize your schema later!) > > Tables: (note plurality) > > parties > ------ > Id > > > parties_roles > ---------- > party_id - fk to party(id) > role_id - fk to party_role_type(id) > > > roles > --------------- > id > name > etc... > > > > *** Note that your join table is alphabetical, and plural for both > models (parties, roles > > > Now on to your classes: > > > class Party < ActiveRecord::Base > has_and_belongs_to_many :roles > end > > class Role < ActiveRecord::Base > has_and_belongs_to_many :parties > end > > > > So now, you can do > > @parties = Role.find(1).parties > > Or > > @roles = Party.find(1).roles > > > > Hope that helps! > > -bph > > > -----Original Message----- > From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Jason Lee > Sent: Friday, October 21, 2005 2:59 PM > To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > Subject: [Rails] habtm question > > > Hi, > > I''ve been following the examples in the Agile Web Development w/Ruby > book and have run across a habtm issue and would like some input. > > I have the following three tables: > > party > ------ > id > > party_role > ---------- > party_id - fk to party(id) > party_role_type_id - fk to party_role_type(id) > > party_role_type > --------------- > id > name > etc... > > On pg 240 of the rails book it describes the following relationship: > > articles > -------- > id > title > > articles_users > -------------- > article_id > user_id > > users > ----- > id > name > > For all practical purposes, my design above mimics this and is a > pretty standard join table. However, when I find() my PartyRole AR > object, it doesn''t automagically connect the two object, it only > provides the local attributes. The AR objects are as follows: > > class Party < ActiveRecord::Base > set_table_name "party" > has_and_belongs_to_many :partyRoleType > end > > class PartyRole < ActiveRecord::Base > set_table_name "party_role" > end > > class PartyRoleType < ActiveRecord::Base > set_table_name "party_role_type" > has_and_belongs_to_many :party > end > > I think my problem here is mostly naming issues and this will > probably be an ongoing issue for me during my migration tests. Does > anyone know of any way that I can specify what the join elements in > this case should be? I''m sure Rails expects ''Roles'' vs. PartyRoleType > in my case and this is what I''m looking to override. I''m having fun > playing w/Rails, but it''s taking a little ''adjustment'' for some of > the AR stuff as my db is pretty much already defined and I can''t > rename or change things easily to match Rails naming conventions. I''m > looking for the middle ground in other words. > > That said, I''d still like to keep moving forward and would appreciate > any input. > > Thanks, > > - jason > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Ah, So the has_and_belongs_to_many method actually accepts arguments, one of which is the join table name via an option param. Wow, perfect. Sorry for the trouble. - jason On Oct 21, 2005, at 1:18 PM, Hogan, Brian P. wrote:> > Okay.... Well take a quick look at the book again.... > > You should try this. I''ve simplified it for you by letting Rails do > the > pluralization (since you''re just starting out, let Rails do the > work.... > Learn to customize your schema later!) > > Tables: (note plurality) > > parties > ------ > Id > > > parties_roles > ---------- > party_id - fk to party(id) > role_id - fk to party_role_type(id) > > > roles > --------------- > id > name > etc... > > > > *** Note that your join table is alphabetical, and plural for both > models (parties, roles > > > Now on to your classes: > > > class Party < ActiveRecord::Base > has_and_belongs_to_many :roles > end > > class Role < ActiveRecord::Base > has_and_belongs_to_many :parties > end > > > > So now, you can do > > @parties = Role.find(1).parties > > Or > > @roles = Party.find(1).roles > > > > Hope that helps! > > -bph > > > -----Original Message----- > From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Jason Lee > Sent: Friday, October 21, 2005 2:59 PM > To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > Subject: [Rails] habtm question > > > Hi, > > I''ve been following the examples in the Agile Web Development w/Ruby > book and have run across a habtm issue and would like some input. > > I have the following three tables: > > party > ------ > id > > party_role > ---------- > party_id - fk to party(id) > party_role_type_id - fk to party_role_type(id) > > party_role_type > --------------- > id > name > etc... > > On pg 240 of the rails book it describes the following relationship: > > articles > -------- > id > title > > articles_users > -------------- > article_id > user_id > > users > ----- > id > name > > For all practical purposes, my design above mimics this and is a > pretty standard join table. However, when I find() my PartyRole AR > object, it doesn''t automagically connect the two object, it only > provides the local attributes. The AR objects are as follows: > > class Party < ActiveRecord::Base > set_table_name "party" > has_and_belongs_to_many :partyRoleType > end > > class PartyRole < ActiveRecord::Base > set_table_name "party_role" > end > > class PartyRoleType < ActiveRecord::Base > set_table_name "party_role_type" > has_and_belongs_to_many :party > end > > I think my problem here is mostly naming issues and this will > probably be an ongoing issue for me during my migration tests. Does > anyone know of any way that I can specify what the join elements in > this case should be? I''m sure Rails expects ''Roles'' vs. PartyRoleType > in my case and this is what I''m looking to override. I''m having fun > playing w/Rails, but it''s taking a little ''adjustment'' for some of > the AR stuff as my db is pretty much already defined and I can''t > rename or change things easily to match Rails naming conventions. I''m > looking for the middle ground in other words. > > That said, I''d still like to keep moving forward and would appreciate > any input. > > Thanks, > > - jason > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Heh, funny, this is exactly what I did too - after more api reading. I think I''m on the right track, but when I assign @roles = Party.find(1).partRoleType I get back nothing - a nil @roles obj. So, still trying. Thanks, - jason On Oct 21, 2005, at 1:29 PM, Hogan, Brian P. wrote:> As a follow-up, since you said that you really can''t change the db, > you''ll have to do a little more tweaking to my example.... > I think the problem you''re having is that you''ve made a model for your > join table. With most HABTM examples, you don''t do that. You''d create > an instance of a Party object and tell it to get roles, and Rails > attempts to find the bridge table for you. > > > > Try this: > > class Party < ActiveRecord::Base > set_table_name "party" > has_and_belongs_to_many :partyRoleType > end > > > class PartyRoleType < ActiveRecord::Base > set_table_name "party_role_type" > has_and_belongs_to_many :party > end > > > Then look at has_and_belongs_to_many to see how to specify the tables > for the join.... I believe it''s has_and_belongs_to_many :party, > :join_table=>"PartyRoles" > > -bph > > > > > > -----Original Message----- > From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Hogan, Brian > P. > Sent: Friday, October 21, 2005 3:18 PM > To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > Subject: RE: [Rails] habtm question > > > > Okay.... Well take a quick look at the book again.... > > You should try this. I''ve simplified it for you by letting Rails do > the > pluralization (since you''re just starting out, let Rails do the > work.... > Learn to customize your schema later!) > > Tables: (note plurality) > > parties > ------ > Id > > > parties_roles > ---------- > party_id - fk to party(id) > role_id - fk to party_role_type(id) > > > roles > --------------- > id > name > etc... > > > > *** Note that your join table is alphabetical, and plural for both > models (parties, roles > > > Now on to your classes: > > > class Party < ActiveRecord::Base > has_and_belongs_to_many :roles > end > > class Role < ActiveRecord::Base > has_and_belongs_to_many :parties > end > > > > So now, you can do > > @parties = Role.find(1).parties > > Or > > @roles = Party.find(1).roles > > > > Hope that helps! > > -bph > > > -----Original Message----- > From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Jason Lee > Sent: Friday, October 21, 2005 2:59 PM > To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > Subject: [Rails] habtm question > > > Hi, > > I''ve been following the examples in the Agile Web Development w/Ruby > book and have run across a habtm issue and would like some input. > > I have the following three tables: > > party > ------ > id > > party_role > ---------- > party_id - fk to party(id) > party_role_type_id - fk to party_role_type(id) > > party_role_type > --------------- > id > name > etc... > > On pg 240 of the rails book it describes the following relationship: > > articles > -------- > id > title > > articles_users > -------------- > article_id > user_id > > users > ----- > id > name > > For all practical purposes, my design above mimics this and is a > pretty standard join table. However, when I find() my PartyRole AR > object, it doesn''t automagically connect the two object, it only > provides the local attributes. The AR objects are as follows: > > class Party < ActiveRecord::Base > set_table_name "party" > has_and_belongs_to_many :partyRoleType > end > > class PartyRole < ActiveRecord::Base > set_table_name "party_role" > end > > class PartyRoleType < ActiveRecord::Base > set_table_name "party_role_type" > has_and_belongs_to_many :party > end > > I think my problem here is mostly naming issues and this will > probably be an ongoing issue for me during my migration tests. Does > anyone know of any way that I can specify what the join elements in > this case should be? I''m sure Rails expects ''Roles'' vs. PartyRoleType > in my case and this is what I''m looking to override. I''m having fun > playing w/Rails, but it''s taking a little ''adjustment'' for some of > the AR stuff as my db is pretty much already defined and I can''t > rename or change things easily to match Rails naming conventions. I''m > looking for the middle ground in other words. > > That said, I''d still like to keep moving forward and would appreciate > any input. > > Thanks, > > - jason > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Well, I guess I''d try this:> class Party < ActiveRecord::Base > set_table_name "party" > has_and_belongs_to_many :partyRoleTypes # note thepluralization!!!!! HAS MANY MUST BE PLURAL> end@roles = Party.find(1).partyroletypes You should get back a collection at that point Test it in script/console and see what you get back for a result. (it''s a lot easier to test stuff in there than it is within a controller) This intrigues me so keep me posted on what you find. I''m on IRC a lot too (hoganbp) so if need help look for me there too. -bph -----Original Message----- From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Jason Lee Sent: Friday, October 21, 2005 5:43 PM To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: Re: [Rails] habtm question Heh, funny, this is exactly what I did too - after more api reading. I think I''m on the right track, but when I assign @roles = Party.find(1).partRoleType I get back nothing - a nil @roles obj. So, still trying. Thanks, - jason On Oct 21, 2005, at 1:29 PM, Hogan, Brian P. wrote:> As a follow-up, since you said that you really can''t change the db, > you''ll have to do a little more tweaking to my example.... I think the> problem you''re having is that you''ve made a model for your join table.> With most HABTM examples, you don''t do that. You''d create an instance> of a Party object and tell it to get roles, and Rails attempts to find> the bridge table for you. > > > > Try this: > > class Party < ActiveRecord::Base > set_table_name "party" > has_and_belongs_to_many :partyRoleTypes > end > > > class PartyRoleType < ActiveRecord::Base > set_table_name "party_role_type" > has_and_belongs_to_many :party > end > > > Then look at has_and_belongs_to_many to see how to specify the tables > for the join.... I believe it''s has_and_belongs_to_many :party, > :join_table=>"PartyRoles" > > -bph > > > > > > -----Original Message----- > From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Hogan, Brian> P. > Sent: Friday, October 21, 2005 3:18 PM > To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > Subject: RE: [Rails] habtm question > > > > Okay.... Well take a quick look at the book again.... > > You should try this. I''ve simplified it for you by letting Rails do > the > pluralization (since you''re just starting out, let Rails do the > work.... > Learn to customize your schema later!) > > Tables: (note plurality) > > parties > ------ > Id > > > parties_roles > ---------- > party_id - fk to party(id) > role_id - fk to party_role_type(id) > > > roles > --------------- > id > name > etc... > > > > *** Note that your join table is alphabetical, and plural for both > models (parties, roles > > > Now on to your classes: > > > class Party < ActiveRecord::Base > has_and_belongs_to_many :roles > end > > class Role < ActiveRecord::Base > has_and_belongs_to_many :parties > end > > > > So now, you can do > > @parties = Role.find(1).parties > > Or > > @roles = Party.find(1).roles > > > > Hope that helps! > > -bph > > > -----Original Message----- > From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Jason Lee > Sent: Friday, October 21, 2005 2:59 PM > To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > Subject: [Rails] habtm question > > > Hi, > > I''ve been following the examples in the Agile Web Development w/Ruby > book and have run across a habtm issue and would like some input. > > I have the following three tables: > > party > ------ > id > > party_role > ---------- > party_id - fk to party(id) > party_role_type_id - fk to party_role_type(id) > > party_role_type > --------------- > id > name > etc... > > On pg 240 of the rails book it describes the following relationship: > > articles > -------- > id > title > > articles_users > -------------- > article_id > user_id > > users > ----- > id > name > > For all practical purposes, my design above mimics this and is a > pretty standard join table. However, when I find() my PartyRole AR > object, it doesn''t automagically connect the two object, it only > provides the local attributes. The AR objects are as follows: > > class Party < ActiveRecord::Base > set_table_name "party" > has_and_belongs_to_many :partyRoleType > end > > class PartyRole < ActiveRecord::Base > set_table_name "party_role" > end > > class PartyRoleType < ActiveRecord::Base > set_table_name "party_role_type" > has_and_belongs_to_many :party > end > > I think my problem here is mostly naming issues and this will probably> be an ongoing issue for me during my migration tests. Does anyone know> of any way that I can specify what the join elements in this case > should be? I''m sure Rails expects ''Roles'' vs. PartyRoleType in my case> and this is what I''m looking to override. I''m having fun playing > w/Rails, but it''s taking a little ''adjustment'' for some of the AR > stuff as my db is pretty much already defined and I can''t rename or > change things easily to match Rails naming conventions. I''m looking > for the middle ground in other words. > > That said, I''d still like to keep moving forward and would appreciate > any input. > > Thanks, > > - jason > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
So I got it to work with the following: class Party < ActiveRecord::Base set_table_name "party" has_and_belongs_to_many :partyRoleType, :join_table => "party_role" end class PartyRoleType < ActiveRecord::Base set_table_name "party_role_type" has_and_belongs_to_many :party end Honestly, I don''t think I even need the PartyRoleType habtm relation to Party as I never go from role types to the party direction. But the habtm for party_role is pretty slick. However, coming from the Java side, I''m still trying to figure things out and a lot of the naming is still confusing. Still have to get used to the underscore method names, instance vars, etc. One thing I don''t quite have a grasp on is the following: In my PartyRoleType class I have a to string method: def to_s str = "role type: [" str += id.to_s + ", " + name + "]" return str end but if I switch it to def to_s str = "role type: [" str += @id.to_s + ", " + name + "]" return str end I don''t get a value in str for the id. I was reading that Ruby uses ''@'' for class level instance vars, which is fine, I get that (in theory). However, when AR wraps up my fields, I''m not 100% sure if i reference them with just plain attribute_name notation or @attribute_name notation (the book tends to use a lot of one word object values which is obviously, simpler than what I have). Plus mix in a little self. and that makes it worse for me. Usually I just keep changing things until the value comes up correctly adn then go back to the books and x-ref. This is obviously not ideal and I''ll just have to live with it until I get a better grasp of the language and tool set. So, anyway, the habtm thing looks solved now. Off to figure out why has_one is not working correctly for me. ;) Thanks for the help. -jason On Oct 21, 2005, at 4:57 PM, Hogan, Brian P. wrote:> Well, I guess I''d try this: > > > >> class Party < ActiveRecord::Base >> set_table_name "party" >> has_and_belongs_to_many :partyRoleTypes # note the >> > pluralization!!!!! HAS MANY MUST BE PLURAL > >> end >> > > @roles = Party.find(1).partyroletypes > > > You should get back a collection at that point > > Test it in script/console and see what you get back for a result. > (it''s > a lot easier to test stuff in there than it is within a controller) > > This intrigues me so keep me posted on what you find. I''m on IRC a lot > too (hoganbp) so if need help look for me there too. > > -bph > > > > -----Original Message----- > From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Jason Lee > Sent: Friday, October 21, 2005 5:43 PM > To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > Subject: Re: [Rails] habtm question > > > Heh, funny, this is exactly what I did too - after more api reading. > > I think I''m on the right track, but when I assign > > @roles = Party.find(1).partRoleType > > I get back nothing - a nil @roles obj. So, still trying. > > Thanks, > > - jason > > On Oct 21, 2005, at 1:29 PM, Hogan, Brian P. wrote: > > >> As a follow-up, since you said that you really can''t change the db, >> you''ll have to do a little more tweaking to my example.... I think >> the >> > > >> problem you''re having is that you''ve made a model for your join >> table. >> > > >> With most HABTM examples, you don''t do that. You''d create an >> instance >> > > >> of a Party object and tell it to get roles, and Rails attempts to >> find >> > > >> the bridge table for you. >> >> >> >> Try this: >> >> class Party < ActiveRecord::Base >> set_table_name "party" >> has_and_belongs_to_many :partyRoleTypes >> end >> >> >> class PartyRoleType < ActiveRecord::Base >> set_table_name "party_role_type" >> has_and_belongs_to_many :party >> end >> >> >> Then look at has_and_belongs_to_many to see how to specify the tables >> for the join.... I believe it''s has_and_belongs_to_many :party, >> :join_table=>"PartyRoles" >> >> -bph >> >> >> >> >> >> -----Original Message----- >> From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Hogan, >> Brian >> > > >> P. >> Sent: Friday, October 21, 2005 3:18 PM >> To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> Subject: RE: [Rails] habtm question >> >> >> >> Okay.... Well take a quick look at the book again.... >> >> You should try this. I''ve simplified it for you by letting Rails do >> the >> pluralization (since you''re just starting out, let Rails do the >> work.... >> Learn to customize your schema later!) >> >> Tables: (note plurality) >> >> parties >> ------ >> Id >> >> >> parties_roles >> ---------- >> party_id - fk to party(id) >> role_id - fk to party_role_type(id) >> >> >> roles >> --------------- >> id >> name >> etc... >> >> >> >> *** Note that your join table is alphabetical, and plural for both >> models (parties, roles >> >> >> Now on to your classes: >> >> >> class Party < ActiveRecord::Base >> has_and_belongs_to_many :roles >> end >> >> class Role < ActiveRecord::Base >> has_and_belongs_to_many :parties >> end >> >> >> >> So now, you can do >> >> @parties = Role.find(1).parties >> >> Or >> >> @roles = Party.find(1).roles >> >> >> >> Hope that helps! >> >> -bph >> >> >> -----Original Message----- >> From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Jason Lee >> Sent: Friday, October 21, 2005 2:59 PM >> To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> Subject: [Rails] habtm question >> >> >> Hi, >> >> I''ve been following the examples in the Agile Web Development w/Ruby >> book and have run across a habtm issue and would like some input. >> >> I have the following three tables: >> >> party >> ------ >> id >> >> party_role >> ---------- >> party_id - fk to party(id) >> party_role_type_id - fk to party_role_type(id) >> >> party_role_type >> --------------- >> id >> name >> etc... >> >> On pg 240 of the rails book it describes the following relationship: >> >> articles >> -------- >> id >> title >> >> articles_users >> -------------- >> article_id >> user_id >> >> users >> ----- >> id >> name >> >> For all practical purposes, my design above mimics this and is a >> pretty standard join table. However, when I find() my PartyRole AR >> object, it doesn''t automagically connect the two object, it only >> provides the local attributes. The AR objects are as follows: >> >> class Party < ActiveRecord::Base >> set_table_name "party" >> has_and_belongs_to_many :partyRoleType >> end >> >> class PartyRole < ActiveRecord::Base >> set_table_name "party_role" >> end >> >> class PartyRoleType < ActiveRecord::Base >> set_table_name "party_role_type" >> has_and_belongs_to_many :party >> end >> >> I think my problem here is mostly naming issues and this will >> probably >> > > >> be an ongoing issue for me during my migration tests. Does anyone >> know >> > > >> of any way that I can specify what the join elements in this case >> should be? I''m sure Rails expects ''Roles'' vs. PartyRoleType in my >> case >> > > >> and this is what I''m looking to override. I''m having fun playing >> w/Rails, but it''s taking a little ''adjustment'' for some of the AR >> stuff as my db is pretty much already defined and I can''t rename or >> change things easily to match Rails naming conventions. I''m looking >> for the middle ground in other words. >> >> That said, I''d still like to keep moving forward and would appreciate >> any input. >> >> Thanks, >> >> - jason >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Jason Lee wrote:> So I got it to work with the following: > > class Party < ActiveRecord::Base > set_table_name "party" > has_and_belongs_to_many :partyRoleType, :join_table => "party_role" > end > > class PartyRoleType < ActiveRecord::Base > set_table_name "party_role_type" > has_and_belongs_to_many :party > end > > Honestly, I don''t think I even need the PartyRoleType habtm relation > to Party as I never go from role types to the party direction. But > the habtm for party_role is pretty slick. However, coming from the > Java side, I''m still trying to figure things out and a lot of the > naming is still confusing. Still have to get used to the underscore > method names, instance vars, etc. One thing I don''t quite have a > grasp on is the following: > > In my PartyRoleType class I have a to string method: > > def to_s > str = "role type: [" > str += id.to_s + ", " + name + "]" > return str > end > > but if I switch it to > > def to_s > str = "role type: [" > str += @id.to_s + ", " + name + "]" > return str > end > > I don''t get a value in str for the id. I was reading that Ruby uses > ''@'' for class level instance vars, which is fine, I get that (in > theory). However, when AR wraps up my fields, I''m not 100% sure if i > reference them with just plain attribute_name notation or > @attribute_name notation (the book tends to use a lot of one word > object values which is obviously, simpler than what I have). Plus mix > in a little self. and that makes it worse for me. Usually I just keep > changing things until the value comes up correctly adn then go back > to the books and x-ref. This is obviously not ideal and I''ll just > have to live with it until I get a better grasp of the language and > tool set.Hi - I''m a relative noob, so I risk exposing myself to ridicule here, but I think I might be able to help a bit. BTW, your technique of playing around til it comes out right is exactly what I did! As I understand it, id is a method. It must be, otherwise it wouldn''t be accessible externally. That''s why @id doesn''t work. self.id works, and, to quote the Pickaxe, "methods in a class can invoke other methods in the same class and its superclasses in functional form (that is, with an implicit receiver of ''self'')" so id works too. At the ActionController/ActionView side of things, I was very confused about instance variables being available in both until someone pointed out that behind the scenes, Rails injects all the current ActionController instance variables into the instance variables of the current ActionView. BTW, in your methods, you don''t need the "return str" line - Ruby methods return the last thing that''s evaluated. Hope this helps a bit :) -- Robert Jones