Belongs_to confusion and some other questions - thanks!

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.

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.

Sure… See below -

<% @sites.each do |site| %> ><%= site.name %> <% end %>

Marcia A. wrote:

Hello,

Could you put your form code here?

Marcia

I think what you want is:

Is there a reason that you aren’t using the form helpers?

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

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:

Is there a reason that you aren’t using the form helpers?

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:

Is there a reason that you aren’t using the form helpers?

Hi,

I’m afraid you have to change the name of the class :frowning:

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

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

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 :slight_smile:

Thanks again!

  • J

Marcia A. wrote:

Hi,

I’m afraid you have to change the name of the class :frowning:

Marcia

Ahh ok, lets try that, i’ll let you know if it works :slight_smile:
Thanks!

Marcia A. wrote:

Hi,

I’m afraid you have to change the name of the class :frowning:

Marcia

I think we’ve discussed this in Real life use of multiple linked models in the same view - Rails - Ruby-Forum

topic name: “Real life use of multiple linked models in the same view”