Hi I suspect that I have found a mistake in the documentation. Either that or I have no idea about relational DBs. http://rails.rubyonrails.com/classes/ActiveRecord/Associations/ClassMeth ods.html The "Is it belongs_to or has_one?" example is surely incorrect. If each post has one author then the posts table would contain the foreign key author_id. Or am I crazy? In the documentation the authors table contains a post_id column. This means that each authors can only have 1 post. Unlikely. Unfortunately, even if I am right it is not helping me solve a (probably simple) association problem where <% @projects.each do |project| %> <tr> <td><%= project.manager.firstname %></td> </tr> <% end %> Is not working Peter (hopefully not crazy) _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Cutting Peter wrote:> Hi > > I suspect that I have found a mistake in the documentation. Either that > or I have no idea about relational DBs. > > http://rails.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html > > The “Is it belongs_to or has_one?” example is surely incorrect. If each > post has one author then the posts table would contain the foreign key > author_id. Or am I crazy? > > In the documentation the authors table contains a post_id column. This > means that each authors can only have 1 post. Unlikely.The documentation is correct, though it may not be a very good example. I think most people, when they see post/author, are going to be thinking an author has many posts, but for this example you are to assume that the author has but one post. In this example think of the post owning the author, not the author owning the post. Back when I was wrapping my head around ARs relationships, I made this diagram: http://curry.elitists.net/~scott/ARAssociations.png> > Unfortunately, even if I am right it is not helping me solve a (probably > simple) association problem where > > <% @projects.each do |project| %> > > <tr> > > <td><%= project.manager.firstname %></td> > > </tr> > > <% end %> > > Is not working >For this, you should have the projects table holding the manager_id FK. You can then have manager either have_one or has_many projects (the FK placement is the same). Is this how you''ve got it set up? -Scott _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
In rails, the table whose model contains belongs_to is the table that contains the foreign key. The case in the doc is a little confusing, but not incorrect. It describes a situation where there is a 1-1 relationship between authors and posts which is not exactly typical. If you''re using the typical model of one author who posts multiple times then you probably want: author has_many :posts post belongs_to :author The doc could probably stand to have a better example, as this was confusing to me too. Brian On Thu, 10 Feb 2005 14:17:48 +0100, Cutting Peter <Peter.Cutting-CwpgxV1jTAxWk0Htik3J/w@public.gmane.org> wrote:> > > > Hi > > I suspect that I have found a mistake in the documentation. Either that or I > have no idea about relational DBs. > > http://rails.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html > > The "Is it belongs_to or has_one?" example is surely incorrect. If each post > has one author then the posts table would contain the foreign key author_id. > Or am I crazy? > > In the documentation the authors table contains a post_id column. This means > that each authors can only have 1 post. Unlikely. > > Unfortunately, even if I am right it is not helping me solve a (probably > simple) association problem where > > <% @projects.each do |project| %> > > <tr> > > <td><%= project.manager.firstname %></td> > > </tr> > > <% end %> > > Is not working > > Peter (hopefully not crazy) > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-- The years ahead pick up their dark bags. They move closer. There''s a slight rise in the silence then nothing. -- (If you''re receiving this in response to mail sent to bluczkie-OM76b2Iv3yLQjUSlxSEPGw@public.gmane.org, don''t be concerned This is my new address, but mail will be forwarded here indefinitely)
Cutting Peter wrote:> Hi > <mailto:rails%40lists.rubyonrails.org?Subject=%5BRails%5D%20documentation%20wrong%3F%3A%20Is%20it%20belongs_to%20or%20has_one%3F&In-Reply-To=5C06CD69C7232049BDB9E9C5F108059D9AFE19%40SELUMBX03.tp1.ad1.tetrapak.com> > > I have never replied to the Rails list so Ill email directly. Hope this > is OK. Does one reply by posting a new email with exactly the correct > subject? I guess so since there is no reply button. >I am not sure how you are reading the list. If by email then the reply should default to going back to the rails list. I''m also posting my reply back to the list for everyone''s benefit.> > Anyways. I have just spent a few hours reading your essays about > relational concepts. Thanks > > > > I reckon your has_one :thumbnail example in the picture > > http://curry..elitists.net/~scott/ARAssociations.png > <http://curry.elitists.net/~scott/ARAssociations.png> > > is slightly over engineered because you could have had a thumbnail_id FK > in the pictures table. This only allows one thumbnail but it complies > with the spec. >I am not sure what you mean by overengineered. If you put the thumbnail FK in the pictures table then you reverse the relationship and it becomes thumbnail has one picture. Everything ActiveRecord expects regarding FKs is expressed in that diagram, they really are that simple.> This simpler approach is the one I am trying to use so I am surprised > that it is proving so difficult to get running in Rails. I have a > project table and a user table. The project table includes a manager_id > FK which should point out the user that is the manager for the project. > Very simple. > > > > project.rb > > class Project < ActiveRecord::Base > > has_one :manager, :class_name => "User" > > end >I think the problem is that you''re reversing the relationship in your head. Don''t think of it as project "has one" manager, but as a project "belongs to" a manager. And, a manager "has many" projects. Try your set up like this: class Project < ActiveRecord::Base belongs_to :manager, :class_name => ''User'' end class User < ActiveRecord::Base has_many :projects end That will make this code behave properly (assuming that @projects comes from something like Project.find_all).> list.rhtml > > <% @projects.each do |project| %> > > <tr> > > <td><%= project.manager.firstname %></td> > > </tr> > > <% end %>_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails