from the docs:
Both express a 1-1 relationship, the difference is mostly where to place
the foreign key, which goes on the table for the class saying
belongs_to. Example:
class Post < ActiveRecord::Base
has_one :author
end
class Author < ActiveRecord::Base
belongs_to :post
end
The tables for these classes could look something like:
CREATE TABLE posts (
id int(11) NOT NULL auto_increment,
title varchar default NULL,
PRIMARY KEY (id)
)
CREATE TABLE authors (
id int(11) NOT NULL auto_increment,
post_id int(11) default NULL,
name varchar default NULL,
PRIMARY KEY (id)
)
so far i follow...
but what if i wanted to do something like...
a show occurs at a venue
a venue can have many shows...
so i have
shows
venue_id
etc.
venue
etc.
so a show belongs_to venue
and what a venue has_many shows
again from the docs...
One-to-one associations
* Assigning an object to a has_one association automatically saves
that object, and the object being replaced (if there is one), in order
to update their primary keys - except if the parent object is unsaved
(new_record? == true).
* If either of these saves fail (due to one of the objects being
invalid) the assignment statement returns false and the assignment is
cancelled.
* If you wish to assign an object to a has_one association without
saving it, use the association.build method (documented below).
* Assigning an object to a belongs_to association does not save the
object, since the foreign key field belongs on the parent. It does not
save the parent either.
* Adding an object to a collection (has_many
<http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M000433>
or has_and_belongs_to_many
<http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M000436>)
automatically saves that object, except if the parent object (the
owner of the collection) is not yet stored in the database.
* If saving any of the objects being added to a collection (via push
or similar) fails, then push returns false.
* You can add an object to a collection without automatically saving
it by using the collection.build method (documented below).
* All unsaved (new_record? == true) members of the collection are
automatically saved when the parent is saved.
so if i did something like venue.shows[10] = show
then the show object gets saved correct?
but show.venue = venue
doesnt result in the venue object getting saved correct?
want to make sure i am reading everything right...
If i have that part right how then do you do something like the following
you have an entity which can have many contact information entries
( similarly a contact info belongs_to an entity )
an entity is either a company or a person so we have a type field to
distinguish
if a customer in an oo sense is a person but in sql is:
CREATE TABLE customers
(
id
entity_id
... additional information above and beyond what defines an entity...
)
how with rails do you represent something as convoluted as this?
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails