My associations are coming out nil


#1

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:

Countryid

44:
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?


#2

you want: has_many :users (note the ‘s’)

hth,
rich

On 2/2/06, Ben M. removed_email_address@domain.invalid wrote:

    belongs_to :country, :foreign_key => "CountryId"

but when I inspect my user class after it has been instanciated, I get this:
44:
http://lists.rubyonrails.org/mailman/listinfo/rails


http://brantinteractive.com
removed_email_address@domain.invalid
4034 skippack pike
v. 267.640.2195
f. 215.689.1454


#3

Do I want that even though I have pluralisation turned off?