Steve V
2005-May-14 23:58 UTC
ActiveRecord::Associations::ClassMethods Documentation Issue
I''m curious, but isn''t this example in the documentation for ActiveRecord::Associations::ClassMethods somewhat counter-intuitive? The SQL DDL is correct for the way it''s used in the models. But isn''t it more logical that an author would have many posts, rather than many authors having one post? From http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.ht ml Example Is it belongs_to or has_one? 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) ) Steve
Jarkko Laine
2005-May-15 07:40 UTC
Re: ActiveRecord::Associations::ClassMethods Documentation Issue
On 15.5.2005, at 02:58, Steve V wrote:> I''m curious, but isn''t this example in the documentation for > ActiveRecord::Associations::ClassMethods somewhat counter-intuitive? > The SQL > DDL is correct for the way it''s used in the models. But isn''t it more > logical that an author would have many posts, rather than many authors > having one post?You''re right, the example is really bad. If you have a better example, could you post a ticket + a patch to http://dev.rubyonrails.com (or if that sounds too much work just send the example to me and I''ll create the patch)? //jarkko -- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Tim Lucas
2005-May-15 09:56 UTC
Re: ActiveRecord::Associations::ClassMethods Documentation Issue
On 15/05/2005, at 9:58 AM, Steve V wrote:> I''m curious, but isn''t this example in the documentation for > ActiveRecord::Associations::ClassMethods somewhat counter-intuitive? > The SQL > DDL is correct for the way it''s used in the models. But isn''t it more > logical that an author would have many posts, rather than many authors > having one post?I agree. I always thought that example was illogical... or at least too subjective. A better example would be one where there is only one logical way to draw the relations between entities. Maybe, say... an MJ CD =) MJ has many cds. A CD has many tracks. A CD has one distributing label. MJ has one fan. There''ll be no confusion about any of those statements ;) - tim lucas