john Bu
2006-May-25 14:42 UTC
[Rails] 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. -- Posted via http://www.ruby-forum.com/.
Marcia Almeida
2006-May-25 17:42 UTC
[Rails] Re: belongs_to confusion and some other questions - thanks!
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.-- Posted via http://www.ruby-forum.com/.
john Bu
2006-May-25 18:14 UTC
[Rails] Re: belongs_to confusion and some other questions - thanks!
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 Almeida wrote:> Hello, > > Could you put your form code here? > > Marcia >-- Posted via http://www.ruby-forum.com/.
john Bu
2006-May-25 19:48 UTC
[Rails] Re: belongs_to confusion and some other questions - thanks!
Is this because its expecting a object back? Tried searching google but no luck. Thanks again.> > Marcia Almeida wrote: >> Hello, >> >> Could you put your form code here? >> >> Marcia >>-- Posted via http://www.ruby-forum.com/.
Paul Barry
2006-May-25 20:29 UTC
[Rails] Re: belongs_to confusion and some other questions - thanks!
I think what you want is: <select name="order[site_id]"> Is there a reason that you aren''t using the form helpers? On 5/25/06, john Bu <burmjohn@gmail.com> wrote:> > > Is this because its expecting a object back? Tried searching google but > no luck. > Thanks again. > > > > Marcia Almeida wrote: > >> Hello, > >> > >> Could you put your form code here? > >> > >> Marcia > >> > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060525/70e01fb2/attachment.html
john Bu
2006-May-25 20:55 UTC
[Rails] Re: Re: belongs_to confusion and some other questions - than
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 Barry wrote:> I think what you want is: > > <select name="order[site_id]"> > > Is there a reason that you aren''t using the form helpers?-- Posted via http://www.ruby-forum.com/.
Marcia Almeida
2006-May-25 23:42 UTC
[Rails] Re: Re: belongs_to confusion and some other questions - than
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 Barry wrote: >> I think what you want is: >> >> <select name="order[site_id]"> >> >> Is there a reason that you aren''t using the form helpers?-- Posted via http://www.ruby-forum.com/.
john Bu
2006-May-26 00:12 UTC
[Rails] Re: Re: belongs_to confusion and some other questions - than
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 -- Posted via http://www.ruby-forum.com/.
Marcia Almeida
2006-May-26 00:34 UTC
[Rails] Re: Re: belongs_to confusion and some other questions - than
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-- Posted via http://www.ruby-forum.com/.
john Bu
2006-May-26 00:42 UTC
[Rails] Re: Re: belongs_to confusion and some other questions - than
Ahh ok, lets try that, i''ll let you know if it works :) Thanks! Marcia Almeida wrote:> Hi, > > I''m afraid you have to change the name of the class :( > > Marcia >-- Posted via http://www.ruby-forum.com/.
john Bu
2006-May-26 02:37 UTC
[Rails] Re: Re: belongs_to confusion and some other questions - than
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 Almeida wrote:> Hi, > > I''m afraid you have to change the name of the class :( > > Marcia >-- Posted via http://www.ruby-forum.com/.