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: [email protected] [mailto:[EMAIL PROTECTED] 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" <[EMAIL PROTECTED]> 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 <
>
> [EMAIL PROTECTED]> wrote:
>
> > On Sep 26, 3:18 pm, "Jon Liu" <[EMAIL PROTECTED]> 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 [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to