Hi all, I''m currently practising with Ruby on Rails at home, getting the hang of how it all works with a message application. I have a question about ActiveRecord mapping. From what I can tell from reading the documentation, ActiveRecord objects are always full entity objects, i.e. they always need a separate id. If I have dependent value objects (for example, a user may have several email addresses) is it possible to model this keeping the email addresses in a separate table with just a foreign key on the email_addresses table? I''d like to avoid making the addresses full entity objects if possible.>From the documentation, it seems like aggregations might be what Iwant, but it seems more like this just creates model objects from separate fields in the same database row. Any and all help gratefully received. Cheers, Colin
Surely this is a simple one to many mapping? Your user model "has_many :emails", your Email model "belongs_to :user" - more details here: http://wiki.rubyonrails.com/rails/show/has_many, with a great example: http://wiki.rubyonrails.com/rails/show/ForumExample. sam On 6/8/05, Colin Fleming <colin.mailinglist-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi all, > > I''m currently practising with Ruby on Rails at home, getting the hang > of how it all works with a message application. I have a question > about ActiveRecord mapping. From what I can tell from reading the > documentation, ActiveRecord objects are always full entity objects, > i.e. they always need a separate id. If I have dependent value objects > (for example, a user may have several email addresses) is it possible > to model this keeping the email addresses in a separate table with > just a foreign key on the email_addresses table? I''d like to avoid > making the addresses full entity objects if possible. > > >From the documentation, it seems like aggregations might be what I > want, but it seems more like this just creates model objects from > separate fields in the same database row. > > Any and all help gratefully received. > > Cheers, > Colin > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- sam http://www.magpiebrain.com/
On Jun 8, 2005, at 10:16 AM, Sam Newman wrote:> Surely this is a simple one to many mapping? Your user model "has_many > :emails", your Email model "belongs_to :user" - more details here: > http://wiki.rubyonrails.com/rails/show/has_many, with a great example: > http://wiki.rubyonrails.com/rails/show/ForumExample. > > sam >I think what he''s getting at is that an Email should be a value object, and thus not have an id, and probably not any finders. e.g. if it is strictly a value object that is part of the User aggregate you oughtn''t be able to say Email.find(:all). But, AR does give you the id and finders, etc. by magic and for free. The options are to just "pretend" you don''t have those freebies, use the AR aggregations (which becomes a little more difficult for collections, as is the case here), or back your entities by something other than AR. Personally I would just use has_many and be done with it, knowing in my head (or documentation) that Emails should be an entity object in the user aggregate. I don''t think AR supports a PK-less table (at least, I have never tried to get it to). -Scott _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Exactly, that was what I was getting at. I have emails mapped as full entity objects at the moment, it just seems a bit daft to have ids that I never use. It''s not a big deal, I just wanted to know if there was a better way. And no, I don''t think there is any way to avoid having the ''id'' field on an entity object, or at least I''ve never found it. Thanks for the responses, Colin On 6/8/05, Scott Barron <scott-HDQKq3lYuGDk1uMJSBkQmQ@public.gmane.org> wrote:> > On Jun 8, 2005, at 10:16 AM, Sam Newman wrote: > > > Surely this is a simple one to many mapping? Your user model "has_many > > :emails", your Email model "belongs_to :user" - more details here: > > http://wiki.rubyonrails.com/rails/show/has_many, with a great example: > > http://wiki.rubyonrails.com/rails/show/ForumExample. > > > > sam > > > > I think what he''s getting at is that an Email should be a value object, > and thus not have an id, and probably not any finders. e.g. if it is > strictly a value object that is part of the User aggregate you oughtn''t > be able to say Email.find(:all). But, AR does give you the id and > finders, etc. by magic and for free. The options are to just "pretend" > you don''t have those freebies, use the AR aggregations (which becomes a > little more difficult for collections, as is the case here), or back > your entities by something other than AR. Personally I would just use > has_many and be done with it, knowing in my head (or documentation) > that Emails should be an entity object in the user aggregate. I don''t > think AR supports a PK-less table (at least, I have never tried to get > it to). > > -Scott > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > >