[Rails] Re: (ActiveRecord) Why is there no "belongs_to_many"?
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...@ruby-forum.com> 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@googlegroups.com.
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.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home