Forum: Ruby on Rails Adding new items with belongs_to/has_many

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
minter (Guest)
on 2005-11-23 18:43
(Received via mailing list)
Originally posted this to the pragmatic studio list.  No response there,
so I'm giving it a shot here.

I'm working on my first post-class Rails app - a rewritten replacement
for
a link-collection site.  Right now, it's a very basic setup with a
Categories table (acts_as_tree), and a Links table.  Table and foreign
keys appear to be set up fine.  I generated a scaffold and did some
minor
modification.  Categories has_many :links, and Links belongs_to
:category.

I've got a helper function that generates a select() statement like
select(:link, :category).  That creates HTML that looks like:

<select id="link_category" name="link[category]">

And the option value is the numeric category id number.

When I submit the form, I get an ActiveRecord error that reads:

Category expected, got String

In the request parameters, I see I'm getting "category" => "8".  I
assume
it's expecting a Category object instead of just the string "8".  In my
scaffold-built controller, the create method is doing the stock:

@link = Link.new(params[:link])
@link.save

Does the scaffold not handle this type of interaction by default?  Do I
need to change that to do something like a @category =
Category.find(params[:category]), and replace the value for "category"
in
the hash with that object?  What's the scoop on how to make those two
classes happy with each other?

--Wade


------------------------ Yahoo! Groups Sponsor --------------------~-->
AIDS in India: A "lurking bomb." Click and help stop AIDS now.
http://us.click.yahoo.com/VpTY2A/lzNLAA/yQLSAA/nhFolB/TM
--------------------------------------------------------------------~->


Yahoo! Groups Links

<*> To visit your group on the web, go to:
     http://groups.yahoo.com/group/pragstudiorails-alumni/

<*> To unsubscribe from this group, send an email to:
     removed_email_address@domain.invalid

<*> Your use of Yahoo! Groups is subject to:
     http://docs.yahoo.com/info/terms/
This topic is locked and can not be replied to.