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.
thats 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 undefined
here '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"
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)
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:…”
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.