Can someone please confirm or correct the following statements? If I have the following tables create table as (id int, [...], b_id int); create table bs (id int, [...], a_id int); create table as_bs (a_id int, b_id int); and the associations woould be defined like this class A << ... habtm :bs belongs_to :b end so my Model A has a habtm collection of Bs *plus* a direct belongs_to link to one B (and vice versa). So a = A.find(:first) b = a.bs.first a2 = b.a might not give me back a, because b's direct belongs_to a reference is clobbered with the reference from the join table a_b (because the foreign keys have the same name)? Or does this only apply to non foreign key attributes? There are no such problems if I'd make a_b a join model and have no habtm, but a has_many :through instead? What other options to circumvent this problem do I have (besides renaming the foreign keys either in the model or join tables)? TIA! - Bernd
on 2006-05-17 14:18
on 2006-05-18 12:58
Not sure I can help but I think you're trying to create a HABTM between model A and model B. That's fairly straightforward. It's the other relationship I'm struggling to understand - is that a 1:1 1:many or many:many? Let's say it's a 1:many, with model A has many Bs. You'd then want has_many in the A model declaration, and belongs_to in the B declaration. Also, you'd only want the foreign key column (a_id) in table bs (and not belongs_to in A, also no foreign key column in as) For sake of clarity, you might want to call this relationship something else (has_many :alt_bs, and then explicitly state the table name and foreign key in the has_many declaration, as per the API). Does this help? If you can supply some more details that'd be good. HTH
on 2006-05-19 12:05
Chris T wrote: > For sake of clarity, you might want to call this relationship something > else (has_many :alt_bs, and then explicitly state the table name and > foreign key in the has_many declaration, as per the API). Although I have some (little) experience with rails associations it seems like I misunderstood the clobbering effects, because I expected them to be "generated" on DB, not rails level. So on other words, there are no ambiguity problems that are causing data clobbering when I'm using the same foreign key names in the model and join tables? (Although I already had this problem when one of the foreign keys in a "legacy" join table has been named "id", but that might have been another problem). Just to answer your question about the other relationship: This could either be a 1:many or 1:1. In my example I used a completely symmetric model. I'll give an example with "real" objects: table clients ( id int, ..., address_id int -- think of this as primary address ); table addresses ( id int, ..., client_id int -- think of this as primary client (although this might not make much sense) ); table addresses_clients ( address_id int, -- same foreign key name as in clients client_id int -- same foreign key name as in addresses ); class Client < ... has_and_belongs_to_many :addresses belongs_to :address # or should it better be primary_address? end class Address < ... has_and_belongs_to_many :clients belongs_to :client # or some other name end Now: c = Client.find(1) a = c.addresses.first # get one from the collection c2 = a.client Assuming that a.client is pointing to c, may I have clobbering effects (a.client being "shadowed" by the client_id from the join table which might point to some other client)?