Consider a one-to-many relationship. Is it valid to declare only the contained collection with has_many, while leaving out the belongs_to on the multiobject side of the relation? In code: class Author < ActiveRecord::Base has_many :articles end class Article < ActiveRecord::Base # Leaving this out: belongs_to :author end Ok or not? -- Florian Ebeling florian.ebeling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
On 04/08/05, Florian Ebeling <florian.ebeling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Consider a one-to-many relationship. Is it valid to declare only the > contained collection with has_many, while leaving out the belongs_to > on the multiobject side of the relation? > > In code: > > class Author < ActiveRecord::Base > has_many :articles > end > class Article < ActiveRecord::Base > # Leaving this out: belongs_to :author > end > > Ok or not? >Hello, I think you really shouldn''t leave out belongs_to, since it creates the foreign_key referencing Author. Best regards, Sebastian A. Espindola.
On Aug 4, 2005, at 8:31 AM, Florian Ebeling wrote:> Consider a one-to-many relationship. Is it valid to declare only the > contained collection with has_many, while leaving out the belongs_to > on the multiobject side of the relation? > > In code: > > class Author < ActiveRecord::Base > has_many :articles > end > class Article < ActiveRecord::Base > # Leaving this out: belongs_to :author > end > > Ok or not?Definitely okay. The database schema remains valid, all this means is that you can''t easily query the author of the article (you''d have to do something like Author.find(article.author_id) instead of just article.author). - Jamis
Don''t we consider the db structure as given and only reflect it into the model at runtime? On 04/08/05, Sebastian A. Espindola <sespindola-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 04/08/05, Florian Ebeling <florian.ebeling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Consider a one-to-many relationship. Is it valid to declare only the > > contained collection with has_many, while leaving out the belongs_to > > on the multiobject side of the relation? > > > > In code: > > > > class Author < ActiveRecord::Base > > has_many :articles > > end > > class Article < ActiveRecord::Base > > # Leaving this out: belongs_to :author > > end > > > > Ok or not? > > > > Hello, > I think you really shouldn''t leave out belongs_to, since it creates the > foreign_key referencing Author. > > Best regards, > Sebastian A. Espindola. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Florian Ebeling florian.ebeling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
Florian Ebeling <florian.ebeling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> writes:> class Author < ActiveRecord::Base > has_many :articles > end > class Article < ActiveRecord::Base > # Leaving this out: belongs_to :author > endYou''re still going to need the author_id column in your article table, right? Otherwise, how''s ActiveRecord to know which articles the author has? As others have said, if you leave out the belongs_to then it''ll be a little more messy accessing the author given an article object. I''m not sure what you hope to gain by leaving out the belongs_to association in the article. You''ve already got the foreign key in the db, why not give up one more line of code in the model to tie it together? -- doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org
Doug Alcorn wrote:> Florian Ebeling <florian.ebeling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> writes: > > >> class Author < ActiveRecord::Base >> has_many :articles >> end >> class Article < ActiveRecord::Base >> # Leaving this out: belongs_to :author >> end > > > You''re still going to need the author_id column in your article table, > right? Otherwise, how''s ActiveRecord to know which articles the > author has? > > As others have said, if you leave out the belongs_to then it''ll be a > little more messy accessing the author given an article object. > > I''m not sure what you hope to gain by leaving out the belongs_to > association in the article. You''ve already got the foreign key in the > db, why not give up one more line of code in the model to tie it > together?Actually I''ve seen situations when the model''s domain logic was against discovering a relationship from one side.. I think (for the original question) that it''s correct to use indicate and use only one side of the relationship in the ruby model. Needless to say, the database still needs to contain the relationship set up with foreign keys correctly. Kristof
My question was only out of a lack of understanding. In fact, it does seem a bit odd to me to have circular references in the domain model. In Java one would have to use some kind of extra thought to get rid of a child. This is because even when you remove the child instance from the parent container, it would still hold a reference onto parent. And that I would consider inconsistentent state. That''s the reason for the detach() method in the JDOM framework. Of cause, there may well be many situations where this is perfectly sensible. Now I wonder where this counter-removoval might take place in AR, and which amount of magic might be involved. Ruby has some very mighty mechnisms in place. (I find ruby/rails shines really bright, in comparison with Java on the web.) It''s just couriosity. I''ll dig a bit. Cheers. On 04/08/05, Kristof Jozsa <dyn-Lj6TOhj6PqOT9ig0jae3mg@public.gmane.org> wrote:> Doug Alcorn wrote: > > Florian Ebeling <florian.ebeling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> writes: > > > > > >> class Author < ActiveRecord::Base > >> has_many :articles > >> end > >> class Article < ActiveRecord::Base > >> # Leaving this out: belongs_to :author > >> end > > > > > > You''re still going to need the author_id column in your article table, > > right? Otherwise, how''s ActiveRecord to know which articles the > > author has? > > > > As others have said, if you leave out the belongs_to then it''ll be a > > little more messy accessing the author given an article object. > > > > I''m not sure what you hope to gain by leaving out the belongs_to > > association in the article. You''ve already got the foreign key in the > > db, why not give up one more line of code in the model to tie it > > together? > > Actually I''ve seen situations when the model''s domain logic was against > discovering a relationship from one side.. > > I think (for the original question) that it''s correct to use indicate and use > only one side of the relationship in the ruby model. Needless to say, the > database still needs to contain the relationship set up with foreign keys correctly. > > Kristof > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Florian Ebeling florian.ebeling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
It''s perfectly fine, as long as you do have the author_id in the articles table. That''s how rails knows how to get all the articles when you call author_name.articles. However, in this particular case it seems like you''d want a reference to the author. You''re going to want to display his name, right? <%= article.author.name %> You''d set it up like you have in cases where the child doesn''t need to or shouldn''t have access to its parent. Pat On 8/4/05, Florian Ebeling <florian.ebeling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> My question was only out of a lack of understanding. > > In fact, it does seem a bit odd to me to have circular references in > the domain model. In Java one would have to use some kind of extra > thought to get rid of a child. This is because even when you remove > the child instance from the parent container, it would still hold a > reference onto parent. And that I would consider inconsistentent > state. > > That''s the reason for the detach() method in the JDOM framework. > > Of cause, there may well be many situations where this is perfectly sensible. > > Now I wonder where this counter-removoval might take place in AR, and > which amount of magic might be involved. Ruby has some very mighty > mechnisms in place. (I find ruby/rails shines really bright, in > comparison with Java on the web.) > > It''s just couriosity. I''ll dig a bit. Cheers. > > On 04/08/05, Kristof Jozsa <dyn-Lj6TOhj6PqOT9ig0jae3mg@public.gmane.org> wrote: > > Doug Alcorn wrote: > > > Florian Ebeling <florian.ebeling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> writes: > > > > > > > > >> class Author < ActiveRecord::Base > > >> has_many :articles > > >> end > > >> class Article < ActiveRecord::Base > > >> # Leaving this out: belongs_to :author > > >> end > > > > > > > > > You''re still going to need the author_id column in your article table, > > > right? Otherwise, how''s ActiveRecord to know which articles the > > > author has? > > > > > > As others have said, if you leave out the belongs_to then it''ll be a > > > little more messy accessing the author given an article object. > > > > > > I''m not sure what you hope to gain by leaving out the belongs_to > > > association in the article. You''ve already got the foreign key in the > > > db, why not give up one more line of code in the model to tie it > > > together? > > > > Actually I''ve seen situations when the model''s domain logic was against > > discovering a relationship from one side.. > > > > I think (for the original question) that it''s correct to use indicate and use > > only one side of the relationship in the ruby model. Needless to say, the > > database still needs to contain the relationship set up with foreign keys correctly. > > > > Kristof > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > -- > Florian Ebeling > florian.ebeling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >