Good afternoon. I have a document and an author table. Due to the fact that a document has an author and an author can have many document, I need to set the association between the two tables, so I add do the Document class the code belong_to: author and to the author class the code has_many :documents. Then I create the view where the user can add a new document. I''ve noticed that if I remove the belongs_to: author from the Document class the application still works. So what''s the use of using the belongs_to? Why do I need it? I apologize for the beginner question, but I''ve search Rails site and can''t seem to get the anwser I wanted. Thanks in advance, Hugo Here is the code: class Author < ActiveRecord::Base has_many :documents end class Document < ActiveRecord::Base belongs_to :author end class DocumentController < ApplicationController scaffold :document def new @authors = Author.find_all end def create item = Document.new # Create a new instance of Todo, so create a new item item.attributes = @params["document"] # The fields of item should be set to what''s in the "new_item" hash #item.author_id = item.attributes["author_id"] if item.save # Try to save our item into the database redirect_to(:action => "list") # Return to the list page if it suceeds else render_text "Couldn''t add new item" # Print an error message otherwise end end end new.html: <h1>New Document</h1> <form action="/document/create" method="post"> <p><label for="document_title">Title</label><br /> <input id="document_title" name="document[title]" size="30" type="text" value="" /></p> <p><label for="document_description">Description</label><br /> <textarea cols="65" id="document_description" name="document[description]" rows="15" wrap="virtual"> </textarea></p> <label for="document_filename">Filename</label><br /> <input id="document_filename" name="document[filename]" size="30" type="text" value="" /></p> <label for="document_author">Author</label><br /> <select id="document_author" name="document[author_id]"> <% for author in @authors %> <option value="<%= author.id %>" selected="selected"><%author.name %></option> <% end %> </select> <input type="submit" value="Create" /></form>
> I have a document and an author table. Due to the fact that a document > has an author and an author can have many document, I need to set the > association between the two tables, so I add do the Document class the > code belong_to: author and to the author class the code has_many > :documents. Then I create the view where the user can add a newThis does not exactly address your question, but here''s an important design consideration - in your app is it possible for a document to have many authors, or will it always be a single author? That will drive your decision to use belongs_to or has_and_belongs_to_many. -TJ
On 5/12/05, TJ Stankus <tjstankus-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I have a document and an author table. Due to the fact that a document > > has an author and an author can have many document, I need to set the > > association between the two tables, so I add do the Document class the > > code belong_to: author and to the author class the code has_many > > :documents. Then I create the view where the user can add a new > > This does not exactly address your question, but here''s an important > design consideration - in your app is it possible for a document to > have many authors, or will it always be a single author? That will > drive your decision to use belongs_to or has_and_belongs_to_many.For now I''m just playing around with Rails to see if I can use it. So, for now the document has only one author.
belongs_to means you have a foreign key. That implies that if you belong_to :authors then the author_id you have in the document table must exist in the authors table (ie a valid author). On 5/12/05, Hugo Magalhaes <hugo.mag-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> So what''s the use of using the belongs_to?-- John W Higgins wishdev-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
> I''ve noticed that if I remove the belongs_to: author from the Document > class the application still works. So what''s the use of using the > belongs_to? Why do I need it? I apologize for the beginner question, > but I''ve search Rails site and can''t seem to get the anwser I wanted.The associations you make simply add some methods to your model that allow you to access the other (associated) model in the way you expect. For example: Suppose we create two objects by finding them in the database: @author = Author.find(1) @document = Document.find(1)> class Author < ActiveRecord::Base > has_many :documents > endThis will allow you to use @author.documents and get a list of valid Document objects.> class Document < ActiveRecord::Base > belongs_to :author > endThis will allow you to use @document.author and get a valid Author object. If your program does not use these (or other accessor methods) to run, then there is technically no need to specify that Author has_many :documents or that Document belongs_to Author. But doing so will, of course, make your intent more readable for others. (Isn''t it nice to see how concise Ruby can be?) Duane Johnson (canadaduane)
Thanks for your anwsers. I think I''ve got the picture. Just need to see or create some examples (use cases) that use this methods to be convinced. :-) On 5/12/05, Duane Johnson <duane.johnson-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I''ve noticed that if I remove the belongs_to: author from the Document > > class the application still works. So what''s the use of using the > > belongs_to? Why do I need it? I apologize for the beginner question, > > but I''ve search Rails site and can''t seem to get the anwser I wanted. > > The associations you make simply add some methods to your model that > allow you to access the other (associated) model in the way you > expect. For example: > > Suppose we create two objects by finding them in the database: > > @author = Author.find(1) > @document = Document.find(1) > > > class Author < ActiveRecord::Base > > has_many :documents > > end > > This will allow you to use @author.documents and get a list of valid > Document objects. > > > class Document < ActiveRecord::Base > > belongs_to :author > > end > > This will allow you to use @document.author and get a valid Author > object. > > If your program does not use these (or other accessor methods) to > run, then there is technically no need to specify that Author > has_many :documents or that Document belongs_to Author. But doing so > will, of course, make your intent more readable for others. (Isn''t > it nice to see how concise Ruby can be?) > > Duane Johnson > (canadaduane) >
On 12.5.2005, at 21:55, Hugo Magalhaes wrote:> Good afternoon. > > I have a document and an author table. Due to the fact that a document > has an author and an author can have many document, I need to set the > association between the two tables, so I add do the Document class the > code belong_to: author and to the author class the code has_many > :documents. Then I create the view where the user can add a new > document. > I''ve noticed that if I remove the belongs_to: author from the Document > class the application still works. So what''s the use of using the > belongs_to? Why do I need it?You tell AR that Author and Document have a one-to-many relationship. In practice that means that you can do something like this: @a = Author.find(1) @docs = @a.documents or @doc = Document.find_first <%= @doc.author.name %> That is, ActiveRecord will build you a whole bunch of association methods for free. See the api docs for more info: http://rails.rubyonrails.com/classes/ActiveRecord/Associations/ ClassMethods.html They have a lot of examples for each association method (has_one, has_many, belongs_to, has_and_belongs_to_many). That said, if you never need to find the author of a document, or never want to know which documents an author has written, you can just leave the associations away. //jarkko> I apologize for the beginner question, > but I''ve search Rails site and can''t seem to get the anwser I wanted. > > Thanks in advance, > Hugo > > Here is the code: > > class Author < ActiveRecord::Base > has_many :documents > end > class Document < ActiveRecord::Base > belongs_to :author > end > > class DocumentController < ApplicationController > scaffold :document > def new > @authors = Author.find_all > end > def create > item = Document.new # Create a new instance of Todo, so create a > new item > item.attributes = @params["document"] # The fields of item should > be set to what''s in the "new_item" hash > #item.author_id = item.attributes["author_id"] > if item.save # Try to save our item into the database > redirect_to(:action => "list") # Return to the list page if > it suceeds > else > render_text "Couldn''t add new item" # Print an error message > otherwise > end > end > end > > new.html: > > <h1>New Document</h1> > <form action="/document/create" method="post"> > <p><label for="document_title">Title</label><br /> > <input id="document_title" name="document[title]" size="30" > type="text" value="" /></p> > <p><label for="document_description">Description</label><br /> > <textarea cols="65" id="document_description" > name="document[description]" rows="15" wrap="virtual"> > </textarea></p> > <label for="document_filename">Filename</label><br /> > <input id="document_filename" name="document[filename]" size="30" > type="text" value="" /></p> > <label for="document_author">Author</label><br /> > <select id="document_author" name="document[author_id]"> > <% for author in @authors %> > <option value="<%= author.id %>" selected="selected"><%> author.name %></option> > <% end %> > </select> > <input type="submit" value="Create" /></form> > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails