I have two models, Review and Album. Album has many reviews, and Review belongs to Album as seen below in their respective model files. class Review < ActiveRecord::Base belongs_to :album end class Album < ActiveRecord::Base has_many :reviews end I then added a form to submit reviews for an album: <% form_for @review do |f| %> <textarea cols="40" id="review_productReview" name="review[productReview]" rows="10"></textarea><br> <input type=''hidden'' id="review_product" name="review[product]" value=''<%= @album.title%>'' > <input type=''hidden'' id="review_productCreator" name="review[productCreator]" value=''<%= @album.artist%>'' > <input name="commit" type="submit" value="Submit Review" /> <% end %> In my controller I have : class ReviewsController < ApplicationController def create @review=Review.new(params[:review]) @review.album=@album @review.save redirect_to ''/inventory'' end end i''m not sure that @review.album=@album is the correct syntax, but even at the command line when I instantiate an object of type Review and do Review.album.title="GoldDigger", it says that album is an undefined method. The only thing I can think of is that I am missing something when I set up the associations. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jon wrote:> def create > @review=Review.new(params[:review]) > @review.album=@albumthats correct syntax (if @album is a object of Album model)> > i''m not sure that @review.album=@album is the correct syntax, but even > at the command line when I instantiate an object of type Review and do > Review.album.title="GoldDigger", it says that album is an undefinedhere ''Review'' is not a model object ,thats a model class and always you can''t get any association with any model class (except if you write your own code inside of that model class) first @review=Review.find(id) unless @review.album @review.album=Album.find(id) end @review.album.title="GoldDigger"> method. The only thing I can think of is that I am missing something > when I set up the associations.-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Sep 26, 5:53 am, Jon <jonli...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> class ReviewsController < ApplicationController > > def create > @review=Review.new(params[:review]) > @review.album=@album > @review.save > redirect_to ''/inventory'' > endThe problem you have is that @album is nil, because you haven''t set it to anything. It may have been set when you rendered the form, but that was a different instance of the controller (quite possibly in a different process or even server). You need to have whatever parameter in the form you need to be able to retrieve that album, (eg a hidden_field) Fred> > end > > i''m not sure that @review.album=@album is the correct syntax, but even > at the command line when I instantiate an object of type Review and do > Review.album.title="GoldDigger", it says that album is an undefined > method. The only thing I can think of is that I am missing something > when I set up the associations.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I tried manually defining @album like so id=1 @review=Review.new(params[:review]) @review.album=Album.find(id) @review.save Because of this, I don''t think it''s that @album was not defined. Also as I brought up before, and I think this is key in showing their is something fundamentally wrong in my set up, the association doesn''t even work at the command line. Ex: a=Album.find(1) r=Review.new r.album=a This yields an error that says "NoMethodError: undefined method ''album='' for #<Review:......" Any insight? On Fri, Sep 26, 2008 at 12:06 AM, Frederick Cheung < frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > > On Sep 26, 5:53 am, Jon <jonli...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > class ReviewsController < ApplicationController > > > > def create > > @review=Review.new(params[:review]) > > @review.album=@album > > @review.save > > redirect_to ''/inventory'' > > end > > The problem you have is that @album is nil, because you haven''t set it > to anything. It may have been set when you rendered the form, but that > was a different instance of the controller (quite possibly in a > different process or even server). You need to have whatever parameter > in the form you need to be able to retrieve that album, (eg a > hidden_field) > > Fred > > > > end > > > > i''m not sure that @review.album=@album is the correct syntax, but even > > at the command line when I instantiate an object of type Review and do > > Review.album.title="GoldDigger", it says that album is an undefined > > method. The only thing I can think of is that I am missing something > > when I set up the associations. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Sep 26, 3:18 pm, "Jon Liu" <jonli...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I tried manually defining @album like so > > id=1 > @review=Review.new(params[:review]) > -dnBBAB2HhyXg6WpnEXGJrQ@public.gmane.org=Album.find(id) > -zwA3+X6BIaLfhZSSa7DgLw@public.gmane.org > > Because of this, I don''t think it''s that @album was not defined. Also as I > brought up before, and I think this is key in showing their is something > fundamentally wrong in my set up, the association doesn''t even work at the > command line. > > Ex: > a=Album.find(1) > r=Review.new > r.album=a > > This yields an error that says "NoMethodError: undefined method ''album='' for > #<Review:......" > > Any insight? >Well the full stacktrace is sometimes helpful. Just to be sure, your reviews table does have an album_id column ? Fred --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
how do you view the stacktrace? I thin this is the solution to my problem...only now migrations is acting funny with me... On Fri, Sep 26, 2008 at 7:27 AM, Frederick Cheung < frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > > On Sep 26, 3:18 pm, "Jon Liu" <jonli...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > I tried manually defining @album like so > > > > id=1 > > @review=Review.new(params[:review]) > > @review.album=Album.find(id) > > @review.save > > > > Because of this, I don''t think it''s that @album was not defined. Also as > I > > brought up before, and I think this is key in showing their is something > > fundamentally wrong in my set up, the association doesn''t even work at > the > > command line. > > > > Ex: > > a=Album.find(1) > > r=Review.new > > r.album=a > > > > This yields an error that says "NoMethodError: undefined method ''album='' > for > > #<Review:......" > > > > Any insight? > > > > Well the full stacktrace is sometimes helpful. Just to be sure, your > reviews table does have an album_id column ? > > Fred > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I''m only guessing here, but I''m thinking maybe that your issue is centred around the way rails automagically creates the setter and getter methods for associations. From what I''m seeing, I''m guessing that has_many creates attr_writer methods whereas belongs_to only creates attr_reader methods. What this means is, album= will not be an accessible method, so trying to set the album within a "child" association isn''t possible (nor should it be). What you need to do is simply set the album_id. I''ve never tried to create associative data the way you are, so I can''t say for sure this is the issue, but I think it''s possible. You generally don''t create relationships via belongs_to. Going the other way, you shouldn''t have any problems though. Ie. @album = Album.new( your params ) @album.reviews = [ Review.new( review1params ), Review.new( review2.params ) ] @album.save .etc.etc. Hope that helps. Kirk "Torm3nt" Bushell On Sep 27, 3:38 am, "Jon Liu" <jonli...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> how do you view the stacktrace? I thin this is the solution to my > problem...only now migrations is acting funny with me... > > On Fri, Sep 26, 2008 at 7:27 AM, Frederick Cheung < > > frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > On Sep 26, 3:18 pm, "Jon Liu" <jonli...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I tried manually defining @album like so > > > > id=1 > > > @review=Review.new(params[:review]) > > > @review.album=Album.find(id) > > > @review.save > > > > Because of this, I don''t think it''s that @album was not defined. Also as > > I > > > brought up before, and I think this is key in showing their is something > > > fundamentally wrong in my set up, the association doesn''t even work at > > the > > > command line. > > > > Ex: > > > a=Album.find(1) > > > r=Review.new > > > r.album=a > > > > This yields an error that says "NoMethodError: undefined method ''album='' > > for > > > #<Review:......" > > > > Any insight? > > > Well the full stacktrace is sometimes helpful. Just to be sure, your > > reviews table does have an album_id column ? > > > Fred--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Nah, you can set a parent from a child. Dig it: >> hh = Album.new(:title => "Houses of the Holy", :artist => "Led Zepplin") => #<Album id: nil, title: "Houses of the Holy", artist: "Led Zepplin", release_date: nil, created_at: nil, updated_at: nil> >> hhr = Review.new(:title => "Led Zep''s new album rocks", :review_body => "Best. Album. Ever.") => #<Review id: nil, title: "Led Zep''s new album rocks", review_body: "Best. Album. Ever.", album_id: nil, created_at: nil, updated_at: nil> >> hh.save => true >> hhr.save => true >> hhr.album = hh => #<Album id: 1, title: "Houses of the Holy", artist: "Led Zepplin", release_date: nil, created_at: "2008-10-13 07:13:04", updated_at: "2008-10-13 07:13:04"> >> hhr.save => true >> -Roy -----Original Message----- From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-talk@googlegroups.com] On Behalf Of torm3nt Sent: Sunday, October 12, 2008 9:08 PM To: Ruby on Rails: Talk Subject: [Rails] Re: has_many / belongs_to associations I''m only guessing here, but I''m thinking maybe that your issue is centred around the way rails automagically creates the setter and getter methods for associations. From what I''m seeing, I''m guessing that has_many creates attr_writer methods whereas belongs_to only creates attr_reader methods. What this means is, album= will not be an accessible method, so trying to set the album within a "child" association isn''t possible (nor should it be). What you need to do is simply set the album_id. I''ve never tried to create associative data the way you are, so I can''t say for sure this is the issue, but I think it''s possible. You generally don''t create relationships via belongs_to. Going the other way, you shouldn''t have any problems though. Ie. @album = Album.new( your params ) @album.reviews = [ Review.new( review1params ), Review.new( review2.params ) ] @album.save .etc.etc. Hope that helps. Kirk "Torm3nt" Bushell On Sep 27, 3:38 am, "Jon Liu" <jonli...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> how do you view the stacktrace? I thin this is the solution to my > problem...only now migrations is acting funny with me... > > On Fri, Sep 26, 2008 at 7:27 AM, Frederick Cheung < > > frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > On Sep 26, 3:18 pm, "Jon Liu" <jonli...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I tried manually defining @album like so > > > > id=1 > > > @review=Review.new(params[:review]) > > > @review.album=Album.find(id) > > > @review.save > > > > Because of this, I don''t think it''s that @album was not defined. > > > Also as > > I > > > brought up before, and I think this is key in showing their is > > > something fundamentally wrong in my set up, the association > > > doesn''t even work at > > the > > > command line. > > > > Ex: > > > a=Album.find(1) > > > r=Review.new > > > r.album=a > > > > This yields an error that says "NoMethodError: undefined method ''album='' > > for > > > #<Review:......" > > > > Any insight? > > > Well the full stacktrace is sometimes helpful. Just to be sure, your > > reviews table does have an album_id column ? > > > Fred--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---