Eric Gross
2006-Jul-27 08:53 UTC
[Rails] Saving records in mySQL for many-to-many relationship
Hey guys,
Maybe someone could point out what wrong here. I''ve simplified the
issue
to this:
Both User.find(6) and Sport.find(1) exist, Im just trying to setup the
relationship between them.
My controller looks like this
def tester
@c = User.find(6)
@c.sports = Sport.find(1)
end
And I keep getting this error:
undefined method `each'' for #<Sport:0x36a3488
@attributes={"name"=>"Basketball",
"id"=>"1"}>
Im pretty sure that I set up the database right. I have a sports_users
table with sport_id and user_id. And when I put records into the
database manually, the records are finding each other, Im just having
trouble getting this to work from a submit form. Anybody encounter this?
--
Posted via http://www.ruby-forum.com/.
Dr Nic
2006-Jul-27 10:00 UTC
[Rails] Re: Saving records in mySQL for many-to-many relationship
> def tester > @c = User.find(6) > @c.sports = Sport.find(1) > endDoes the users table have a sports_id column? -- Posted via http://www.ruby-forum.com/.
Mark Reginald James
2006-Jul-27 12:52 UTC
[Rails] Re: Saving records in mySQL for many-to-many relationship
Eric Gross wrote:> @c.sports = Sport.find(1) > > And I keep getting this error: > > undefined method `each'' for #<Sport:0x36a3488 > @attributes={"name"=>"Basketball", "id"=>"1"}>sports is a collection/array, so you want @c.sports << Sport.find(1) -- We develop, watch us RoR, in numbers too big to ignore.