Forum: Ruby on Rails belongs_to confusion and some other questions - thanks!

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.
John B. (Guest)
on 2006-05-25 18:42
Hello all,

Thanks for the help in advance, I am having some trouble with
belongs_to, and I caint seem to figure it out.

I have two tables, one called 'pow_users' and one is called 'pow_site'

pow_users has a field called site that has the site ID for primary id in
the pow_site' table.

the pow_site table setup as the Site class with the following:

class Site < ActiveRecord::Base
set_table_name 'pow_sites'
end


So in my orders class I have the following:

class Order < ActiveRecord::Base

set_table_name 'pow_users'
belongs_to  :site,
            :foreign_key => 'site'

end

When I do for example <%= orders.site.name %> it works fine and shows
the site name... However if I try to save a form like with the variable
as order[site] i receive the following error: "Site expected, got
String"

Thanks again! Let me know if you need more info to understand my mess.
Marcia A. (Guest)
on 2006-05-25 21:39
Hello,

Could you put your form code here?

Marcia

john Bu wrote:
> Hello all,
>
> Thanks for the help in advance, I am having some trouble with
> belongs_to, and I caint seem to figure it out.
>
> I have two tables, one called 'pow_users' and one is called 'pow_site'
>
> pow_users has a field called site that has the site ID for primary id in
> the pow_site' table.
>
> the pow_site table setup as the Site class with the following:
>
> class Site < ActiveRecord::Base
> set_table_name 'pow_sites'
> end
>
>
> So in my orders class I have the following:
>
> class Order < ActiveRecord::Base
>
> set_table_name 'pow_users'
> belongs_to  :site,
>             :foreign_key => 'site'
>
> end
>
> When I do for example <%= orders.site.name %> it works fine and shows
> the site name... However if I try to save a form like with the variable
> as order[site] i receive the following error: "Site expected, got
> String"
>
> Thanks again! Let me know if you need more info to understand my mess.
John B. (Guest)
on 2006-05-25 22:13
Sure.. See below -

<select name="order[site]">
<% @sites.each do |site| %>
<option value="<%= site.id %>"<%= ' selected="selected"' if site.id ==
order.site.id %>><%= site.name %></option>
<% end %>
</select>




Marcia A. wrote:
> Hello,
>
> Could you put your form code here?
>
> Marcia
>
John B. (Guest)
on 2006-05-25 23:45
Is this because its expecting a object back? Tried searching google but
no luck.
Thanks again.
>
> Marcia A. wrote:
>> Hello,
>>
>> Could you put your form code here?
>>
>> Marcia
>>
Paul B. (Guest)
on 2006-05-26 00:29
(Received via mailing list)
I think what you want is:

<select name="order[site_id]">

Is there a reason that you aren't using the form helpers?
John B. (Guest)
on 2006-05-26 00:52
I did try the order[site_id] however I get this error:

undefined method `site_id=' for #<Order:0xb77cf6e8>

I tried the helpers however I had a issue of it not doing "selected"
correctly.

Thanks
 - J



Paul B. wrote:
> I think what you want is:
>
> <select name="order[site_id]">
>
> Is there a reason that you aren't using the form helpers?
Marcia A. (Guest)
on 2006-05-26 03:42
Hello,

Try to change

 belongs_to  :site,
             :foreign_key => 'site'

to

 belongs_to  :site,
             :foreign_key => 'site_id'

Problems appear if you have relationship and foreign_key with the same
name. And  don't forget to change the field name in your database too.

Marcia



john Bu wrote:
> I did try the order[site_id] however I get this error:
>
> undefined method `site_id=' for #<Order:0xb77cf6e8>
>
> I tried the helpers however I had a issue of it not doing "selected"
> correctly.
>
> Thanks
>  - J
>
>
>
> Paul B. wrote:
>> I think what you want is:
>>
>> <select name="order[site_id]">
>>
>> Is there a reason that you aren't using the form helpers?
John B. (Guest)
on 2006-05-26 04:11
Hello,

Is there a way to get around that because its a legacy DB, and I caint
change that field (the main website depends on it right now).

Thanks
 - J
Marcia A. (Guest)
on 2006-05-26 04:34
Hi,

I'm afraid you have to change the name of the class :(

Marcia

john Bu wrote:
> Hello,
>
> Is there a way to get around that because its a legacy DB, and I caint
> change that field (the main website depends on it right now).
>
> Thanks
>  - J
John B. (Guest)
on 2006-05-26 04:41
Ahh ok, lets try that, i'll let you know if it works :)
Thanks!

Marcia A. wrote:
> Hi,
>
> I'm afraid you have to change the name of the class :(
>
> Marcia
>
John B. (Guest)
on 2006-05-26 06:37
Hello,

I changed the name of the class to website rather then site and changed
all the old references from site to website.  However, when i use <%=
order.site.name %> this no longer functions (undefined method `name' for
"2":String).

Almost there :)

Thanks again!
- J

Marcia A. wrote:
> Hi,
>
> I'm afraid you have to change the name of the class :(
>
> Marcia
>
Mislav (Guest)
on 2006-05-26 16:05
I think we've discussed this in http://www.ruby-forum.com/topic/66876

topic name: "Real life use of multiple linked models in the same view"
This topic is locked and can not be replied to.