Hi all, My apologies, but I am just starting with Rails. I''m curious as to why there is no "belongs_to_many" as opposed to the current need to contruct a relationship that basically means the same thing with more steps. Any ideas? Regards, Mike. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2006-Oct-11 03:29 UTC
Re: (ActiveRecord) Why is there no "belongs_to_many"?
Hi -- On Wed, 11 Oct 2006, gberz3 wrote:> > Hi all, > > My apologies, but I am just starting with Rails. I''m curious as to why > there is no "belongs_to_many" as opposed to the current need to > contruct a relationship that basically means the same thing with more > steps.If you have an X that belongs to a Y, that means you''ll have a y_id field in the xs (plural of x) table. If the X belongs to more than one Y, you have to have multiple y_id fields for each record. Since the xs table can only have one y_id column, you have to put the multiple y_ids in a separate table. Each row in that table also gets the corresponding id key from the xes table. David -- David A. Black | dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] DABlog (DAB''s Weblog) [2] | Co-director, Ruby Central, Inc. [4] [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
gberz3 wrote:> I''m curious as to why > there is no "belongs_to_many" as opposed to the current need to > contruct a relationship that basically means the same thing with more > steps.There is. It''s called has_and_belongs_to_many. :) Besides that, "belonging to many" is merely the flip side of having many. You can''t truly belong to many without also having many. Have implies a direct ownership, and belongs implies being owned. HABTM goes both directions. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Check out this diagram: http://mboffin.com/stuff/ruby-on-rails-data-relationships.png It might make the relatioships a bit clearer. --Dylan --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2006-Oct-11 11:57 UTC
Re: (ActiveRecord) Why is there no "belongs_to_many"?
Hi -- On Wed, 11 Oct 2006, Daniel Waite wrote:> > gberz3 wrote: >> I''m curious as to why >> there is no "belongs_to_many" as opposed to the current need to >> contruct a relationship that basically means the same thing with more >> steps. > > There is. It''s called has_and_belongs_to_many. :) > > Besides that, "belonging to many" is merely the flip side of having > many. You can''t truly belong to many without also having many.They''re not symmetrical, though: an object can have_many even if each of the "havees" doesn''t belong to many. David -- David A. Black | dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] DABlog (DAB''s Weblog) [2] | Co-director, Ruby Central, Inc. [4] [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
has_one|many creates methods based on the assumption that the other model has a field referencing this model, and belongs_to creates methods based on the assumption that this model has a field referencing the other model. So a belongs_to_many macro would create methods based on the assumption that this model has (infinitely) many fields referencing the other model -- which is more easily implemented with a join table, thus habtm or has_many :through. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Hi Ashley, Ashley Thomas wrote:> has_one|many creates methods based on the assumption that the other > model has a field referencing this model, and belongs_to creates methods > based on the assumption that this model has a field referencing the > other model. So a belongs_to_many macro would create methods based on > the assumption that this model has (infinitely) many fields referencing > the other model -- which is more easily implemented with a join table, > thus habtm or has_many :through.Nice explanation. Best regards, Bill --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
I understand, but what if I don''t necessarily want "both ways"? For instance, in the Oreilly book "Ruby on Rails: Up and Running" they have the example of a Slideshow. The Slideshow has_many Slides, and Photos has_many Slides, but Slides belongs_to both Slideshow and Photos. Despite how the items are defined, in English this doesn''t seem correct. It seems more likely that, if nothing else, Photos "belongs_to_many" Slides, more than Photos "has_many" Slides, like so: Slideshow has_many Slides Slide has_one Photo Slide belongs_to Slideshow Photo belongs_to_many Slides I don''t see any need for HABTM, simply, BTM. What am I missing? Thanks, Mike Daniel Waite wrote:> gberz3 wrote: > > I''m curious as to why > > there is no "belongs_to_many" as opposed to the current need to > > contruct a relationship that basically means the same thing with more > > steps. > > There is. It''s called has_and_belongs_to_many. :) > > Besides that, "belonging to many" is merely the flip side of having > many. You can''t truly belong to many without also having many. > > Have implies a direct ownership, and belongs implies being owned. HABTM > goes both directions. > > -- > Posted via http://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2006-Oct-12 00:30 UTC
Re: (ActiveRecord) Why is there no "belongs_to_many"?
Hi -- On Wed, 11 Oct 2006, gberz3 wrote:> > Daniel Waite wrote: >> gberz3 wrote: >>> I''m curious as to why >>> there is no "belongs_to_many" as opposed to the current need to >>> contruct a relationship that basically means the same thing with more >>> steps. >> >> There is. It''s called has_and_belongs_to_many. :) >> >> Besides that, "belonging to many" is merely the flip side of having >> many. You can''t truly belong to many without also having many. >> >> Have implies a direct ownership, and belongs implies being owned. HABTM >> goes both directions. >> > > I understand, but what if I don''t necessarily want "both ways"? For > instance, in the Oreilly book "Ruby on Rails: Up and Running" they have > the example of a Slideshow. The Slideshow has_many Slides, and Photos > has_many Slides, but Slides belongs_to both Slideshow and Photos. > Despite how the items are defined, in English this doesn''t seem > correct. It seems more likely that, if nothing else, Photos > "belongs_to_many" Slides, more than Photos "has_many" Slides, like so: > > Slideshow has_many Slides > Slide has_one Photo > Slide belongs_to Slideshow > Photo belongs_to_many Slides > > I don''t see any need for HABTM, simply, BTM. What am I missing?I''m not sure I follow. It looks like it''s just one-to-many, not many-to-many, so it''s not a real test of whether habtm might be useful. Remember too that the "have/belong" wording is really for the purpose of establishing object-relational mappings, not fully documenting the application. The available relations -- has_one, has_many, etc. -- will never accurately describe what''s going on in every domain. You''d really want to say: class Slide < AR::Base has_been_made_from :photo or something like that, but we can''t :-) The have/belong thing is a simple mechanism that really just bootstraps a more semantically rich one. I also don''t see a way to implement belongs_to_many, in database terms consistent with ActiveRecord''s logic. Where would you put the foreign keys? David -- David A. Black | dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] DABlog (DAB''s Weblog) [2] | Co-director, Ruby Central, Inc. [4] [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Basically, I''d say this: class Photo < AR::Base belongs_to_many :slides end . . .and perhaps the following if AR couldn''t figure it out class Slide < AR::Base has_one :photo end . . .and AR should know to put the FK into Slides. Does that make sense? Mike dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org wrote:> Hi -- > > On Wed, 11 Oct 2006, gberz3 wrote: > > > > > Daniel Waite wrote: > >> gberz3 wrote: > >>> I''m curious as to why > >>> there is no "belongs_to_many" as opposed to the current need to > >>> contruct a relationship that basically means the same thing with more > >>> steps. > >> > >> There is. It''s called has_and_belongs_to_many. :) > >> > >> Besides that, "belonging to many" is merely the flip side of having > >> many. You can''t truly belong to many without also having many. > >> > >> Have implies a direct ownership, and belongs implies being owned. HABTM > >> goes both directions. > >> > > > > I understand, but what if I don''t necessarily want "both ways"? For > > instance, in the Oreilly book "Ruby on Rails: Up and Running" they have > > the example of a Slideshow. The Slideshow has_many Slides, and Photos > > has_many Slides, but Slides belongs_to both Slideshow and Photos. > > Despite how the items are defined, in English this doesn''t seem > > correct. It seems more likely that, if nothing else, Photos > > "belongs_to_many" Slides, more than Photos "has_many" Slides, like so: > > > > Slideshow has_many Slides > > Slide has_one Photo > > Slide belongs_to Slideshow > > Photo belongs_to_many Slides > > > > I don''t see any need for HABTM, simply, BTM. What am I missing? > > I''m not sure I follow. It looks like it''s just one-to-many, not > many-to-many, so it''s not a real test of whether habtm might be > useful. > > Remember too that the "have/belong" wording is really for the purpose > of establishing object-relational mappings, not fully documenting the > application. The available relations -- has_one, has_many, etc. -- > will never accurately describe what''s going on in every domain. You''d > really want to say: > > class Slide < AR::Base > has_been_made_from :photo > > or something like that, but we can''t :-) The have/belong thing is a > simple mechanism that really just bootstraps a more semantically rich > one. > > I also don''t see a way to implement belongs_to_many, in database terms > consistent with ActiveRecord''s logic. Where would you put the foreign > keys? > > > David > > -- > David A. Black | dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org > Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] > DABlog (DAB''s Weblog) [2] | Co-director, Ruby Central, Inc. [4] > [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com > [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Hello, I am re-opening this again, sorry :). I have just read in RailsGuides ( http://guides.rubyonrails.org/association_basics.html ) about the proper way to choose between belongs_to and has_one. From this point of view, something like belongs_to_many seems to be really missing, but i do not know if maybe there is a better name, and how to call the other side of the association (has_a ?). I am talking about something like this: class Member < ActiveRecord::Base has_an :address end class Address < ActiveRecord::Base belongs_to_many :members end Different members can have the same address, if they are a couple for example. This can me implemented the same way as has_and_belongs_to_many, except that in the joint table the Foreign Key for Members must be required to be unique. An Address can be then automatically deleted when the last Member living at that address is deleted. The advantage over [belongs_to, has_many] is the same as for has_one over belongs_to: the Members table does not need to "know" about the Addresses table. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Phil Crissman
2011-Mar-06 00:03 UTC
Re: Re: (ActiveRecord) Why is there no "belongs_to_many"?
On Sat, Mar 5, 2011 at 4:13 PM, Alexey M. <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hello, I am re-opening this again, sorry :). > > I have just read in RailsGuides ( > http://guides.rubyonrails.org/association_basics.html ) > about the proper way to choose between belongs_to and has_one. > From this point of view, something like belongs_to_many seems to be > really missing, but i do not know if maybe there is a better name, and > how to call the other side of the association (has_a ?). > I am talking about something like this: > > class Member < ActiveRecord::Base > has_an :address > end > > class Address < ActiveRecord::Base > belongs_to_many :members > end > > Different members can have the same address, if they are a couple for > example. > > This can me implemented the same way as has_and_belongs_to_many, except > that in the joint table the Foreign Key for Members must be required to > be unique. > > An Address can be then automatically deleted when the last Member living > at that address is deleted. > > The advantage over [belongs_to, has_many] is the same as for has_one > over belongs_to: the Members table does not need to "know" about the > Addresses table. >The thing is, this would just duplicate existing associations; I don''t see how it''s needed. The relationship you describe above can be written much simpler (no join table at all) by just having User belongs_to :address, and Address has_many :users. If it''s the readability of the code that you want, you can always do things like class Address << ActiveRecord::Base has_many :residents, :class_name => "User" end class User << ActiveRecord::Base belongs_to :address, :foreign_key => "resident_id" end For many-to-many, I really tend to favor has_many :through associations from both sides, as they''re more flexible. But honestly; I don''t think belongs_to_many is missing. Just my 2¢. :-)> -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Can you please show the database layout you have in mind that this would map to? The normal way would be: User (belongs_to :address) - id - address_id Address (has_many :users) - id If you want the user to not know about the address, you could do: User Addressable - address_id - object_id - object_type Address and then user and address would both use have_* :through. On Mar 5, 5:13 pm, "Alexey M." <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hello, I am re-opening this again, sorry :). > > I have just read in RailsGuides (http://guides.rubyonrails.org/association_basics.html) > about the proper way to choose between belongs_to and has_one. > From this point of view, something like belongs_to_many seems to be > really missing, but i do not know if maybe there is a better name, and > how to call the other side of the association (has_a ?). > I am talking about something like this: > > class Member < ActiveRecord::Base > has_an :address > end > > class Address < ActiveRecord::Base > belongs_to_many :members > end > > Different members can have the same address, if they are a couple for > example. > > This can me implemented the same way as has_and_belongs_to_many, except > that in the joint table the Foreign Key for Members must be required to > be unique. > > An Address can be then automatically deleted when the last Member living > at that address is deleted. > > The advantage over [belongs_to, has_many] is the same as for has_one > over belongs_to: the Members table does not need to "know" about the > Addresses table. > > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thanks for your replies. Probably there are ways around (i have not finished reading the RailsGuides yet), but what seems to be nice to have is: 1. not to store any foreign key in Members table, 2. have a kind of (:dependent => ...) option that would allow to automatically delete an Address when the last Member living at that address is deleted. Adam, was your question addressed to me or to Phil? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
David A. Black wrote in post #712598:> Hi -- > > On Wed, 6 Aug 2008, Shawn .. wrote: > > > The has/belongs semantics are, indeed, not always a perfect fit for > the way things relate when we describe them in English. For example, I > remember coming up with an example once where I ended up with a Club > belonging to a Member (or something like that; I''m not remembering > exactly), whereas normally we''d say "He belongs to that club." > > My advice would be to use it as is for a while. If you want two-way > has_many''s, then just use has_many. Remember that the main point of > associations, in any case, is that they generate methods for you. It''s > nice to have them as close as possible to the domain you''re modeling, > but it''s often not very close since "has" and "belongs to" are not > general terms for expressing every possible connection between two > things. > > In the end, I think it''s better to keep the number of association > methods down, partly because it helps when it comes to learning how > the associations themselves behave -- which ones trigger automatic > saves, and when, and things like that. > > > DavidI am very grateful for this response. I, too was having a bit of difficulty earlier when the "English just wasn''t making sense". See, I have an Event model and a DressCode model and I want to say that Event has_one DressCode, but I put the foreign key (dress_code_id) in the Event model (just as I would in my usual SQL/PHP ways. It turned out that I had to say DressCode has_many Events and Event belongs_to DressCode. Unorthodox but it works because now I can run Event.dress_code and I get the result i''m looking for. Rails is so advanced that I sometimes forget that it is still just a programming language and, as you said, can''t always be semantically correct all the time when illustrating real world relationships. I spent a while searching, ended up on this thread and I finally got my head around it. To the original poster - just take the advice and do what needs to be done in order to satisfy the rails convention. Reading your code might be weird, but that''s nothing that a few comments can''t fix ;) -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Just use a couple of aliases if you''re so bothered by the lack of semantic readability. belongs_to_many = has_many has_a = belongs_to but beware that in your need to make the code more semantically readable, you''ve just confounded a seasoned railer. :) -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.