I''ve got a fairly simple system with a table for my Users that
includes a CountryId field which looks up to the CountryId field of my
Country table. What I would like to happen is for my Rails
application, which contains models for both User and Country, to be
able to draw the dropdown list of available countries on the edit
page, like it does in all the tutorials.
My model classes look like this:
class User < ActiveRecord::Base
belongs_to :country, :foreign_key => "CountryId"
set_primary_key "userId"
end
and
class Country < ActiveRecord::Base
has_many :user
set_primary_key "countryId"
end
but when I inspect my user class after it has been instanciated, I get this:
inspect: #<User:0x4062a68 @attributes={"userName"=>"sese
daxnebwow",
"CountryId"=>"216",
"userId"=>"122",
"userIP"=>"127.0.0.1"}>
And then I get this in my page:
ActionView::TemplateError (undefined method `each'' for nil:NilClass)
on line #45 of app/views/users/_form.rhtml:
42:
43: <p><label
for="user_CountryId">Countryid</label><br/>
44: <select name="user_CountryId">
45: <% @country.each do |country| %>
46: <option value="<%=country.id %>"
47: <%= '' selected'' if country.id ==
@user.country_id %>>
48: <%= country.countryname %>
Does anyone have any idea of what I''m doing wrong, or why I''m
not
getting any obvious result from the association?
you want: has_many :users (note the ''s'') hth, rich On 2/2/06, Ben Moxon <glenatron@gmail.com> wrote:> I''ve got a fairly simple system with a table for my Users that > includes a CountryId field which looks up to the CountryId field of my > Country table. What I would like to happen is for my Rails > application, which contains models for both User and Country, to be > able to draw the dropdown list of available countries on the edit > page, like it does in all the tutorials. > > My model classes look like this: > > class User < ActiveRecord::Base > belongs_to :country, :foreign_key => "CountryId" > set_primary_key "userId" > end > > and > > class Country < ActiveRecord::Base > has_many :user > set_primary_key "countryId" > end > > but when I inspect my user class after it has been instanciated, I get this: > > inspect: #<User:0x4062a68 @attributes={"userName"=>"sese daxnebwow", > "CountryId"=>"216", "userId"=>"122", "userIP"=>"127.0.0.1"}> > > And then I get this in my page: > > ActionView::TemplateError (undefined method `each'' for nil:NilClass) > on line #45 of app/views/users/_form.rhtml: > 42: > 43: <p><label for="user_CountryId">Countryid</label><br/> > 44: <select name="user_CountryId"> > 45: <% @country.each do |country| %> > 46: <option value="<%=country.id %>" > 47: <%= '' selected'' if country.id == @user.country_id %>> > 48: <%= country.countryname %> > > Does anyone have any idea of what I''m doing wrong, or why I''m not > getting any obvious result from the association? > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- http://brantinteractive.com rbrant@brantinteractive.com 4034 skippack pike v. 267.640.2195 f. 215.689.1454
Do I want that even though I have pluralisation turned off? On 2/3/06, Rich Brant <rbrant@gmail.com> wrote:> you want: has_many :users (note the ''s'') > > hth, > rich > > On 2/2/06, Ben Moxon <glenatron@gmail.com> wrote: > > I''ve got a fairly simple system with a table for my Users that > > includes a CountryId field which looks up to the CountryId field of my > > Country table. What I would like to happen is for my Rails > > application, which contains models for both User and Country, to be > > able to draw the dropdown list of available countries on the edit > > page, like it does in all the tutorials. > > > > My model classes look like this: > > > > class User < ActiveRecord::Base > > belongs_to :country, :foreign_key => "CountryId" > > set_primary_key "userId" > > end > > > > and > > > > class Country < ActiveRecord::Base > > has_many :user > > set_primary_key "countryId" > > end > > > > but when I inspect my user class after it has been instanciated, I get this: > > > > inspect: #<User:0x4062a68 @attributes={"userName"=>"sese daxnebwow", > > "CountryId"=>"216", "userId"=>"122", "userIP"=>"127.0.0.1"}> > > > > And then I get this in my page: > > > > ActionView::TemplateError (undefined method `each'' for nil:NilClass) > > on line #45 of app/views/users/_form.rhtml: > > 42: > > 43: <p><label for="user_CountryId">Countryid</label><br/> > > 44: <select name="user_CountryId"> > > 45: <% @country.each do |country| %> > > 46: <option value="<%=country.id %>" > > 47: <%= '' selected'' if country.id == @user.country_id %>> > > 48: <%= country.countryname %> > > > > Does anyone have any idea of what I''m doing wrong, or why I''m not > > getting any obvious result from the association? > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > -- > http://brantinteractive.com > rbrant@brantinteractive.com > 4034 skippack pike > v. 267.640.2195 > f. 215.689.1454 > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Well, I got it working, with a "has_may :users" in the country model and a "belongs_to :country" in the user one, but Rails didn''t do anything for me of it''s own accord I had to tell it to draw the select box manually in the _form.rhtml file. Still quite new to the platform and using "generate scaffold" to create my views and so on rather than doing it on the fly, which I think is causing some of the problems I''ve been having as I don''t always know what to expect from it... -ben On 2/3/06, Rich Brant <rbrant@brantinteractive.com> wrote:> good question :) it''d be easy to check. my guess is the > pluralisation is more about the type of relationship. i.e., a country > has many users, indicating its a one to many. do post back when you > figure it out. > > On 2/3/06, Ben Moxon <glenatron@gmail.com> wrote: > > Do I want that even though I have pluralisation turned off? > > > > On 2/3/06, Rich Brant <rbrant@gmail.com> wrote: > > > you want: has_many :users (note the ''s'') > > > > > > hth, > > > rich > > > > > > On 2/2/06, Ben Moxon <glenatron@gmail.com> wrote: > > > > I''ve got a fairly simple system with a table for my Users that > > > > includes a CountryId field which looks up to the CountryId field of my > > > > Country table. What I would like to happen is for my Rails > > > > application, which contains models for both User and Country, to be > > > > able to draw the dropdown list of available countries on the edit > > > > page, like it does in all the tutorials. > > > > > > > > My model classes look like this: > > > > > > > > class User < ActiveRecord::Base > > > > belongs_to :country, :foreign_key => "CountryId" > > > > set_primary_key "userId" > > > > end > > > > > > > > and > > > > > > > > class Country < ActiveRecord::Base > > > > has_many :user > > > > set_primary_key "countryId" > > > > end > > > > > > > > but when I inspect my user class after it has been instanciated, I get this: > > > > > > > > inspect: #<User:0x4062a68 @attributes={"userName"=>"sese daxnebwow", > > > > "CountryId"=>"216", "userId"=>"122", "userIP"=>"127.0.0.1"}> > > > > > > > > And then I get this in my page: > > > > > > > > ActionView::TemplateError (undefined method `each'' for nil:NilClass) > > > > on line #45 of app/views/users/_form.rhtml: > > > > 42: > > > > 43: <p><label for="user_CountryId">Countryid</label><br/> > > > > 44: <select name="user_CountryId"> > > > > 45: <% @country.each do |country| %> > > > > 46: <option value="<%=country.id %>" > > > > 47: <%= '' selected'' if country.id == @user.country_id %>> > > > > 48: <%= country.countryname %> > > > > > > > > Does anyone have any idea of what I''m doing wrong, or why I''m not > > > > getting any obvious result from the association? > > > > _______________________________________________ > > > > Rails mailing list > > > > Rails@lists.rubyonrails.org > > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > > > > > -- > > > http://brantinteractive.com > > > rbrant@brantinteractive.com > > > 4034 skippack pike > > > v. 267.640.2195 > > > f. 215.689.1454 > > > _______________________________________________ > > > Rails mailing list > > > Rails@lists.rubyonrails.org > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > -- > http://brantinteractive.com > rbrant@brantinteractive.com > 4034 skippack pike > v. 267.640.2195 > f. 215.689.1454 >