Forum: Ruby on Rails My associations are coming out nil.

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Ben M. (Guest)
on 2006-02-03 02:28
(Received via mailing list)
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"


class Country < ActiveRecord::Base
        has_many :user
	set_primary_key "countryId"

but when I inspect my user class after it has been instanciated, I get

inspect: #<User:0x4062a68 @attributes={"userName"=>"sese daxnebwow",
"CountryId"=>"216", "userId"=>"122", "userIP"=>""}>

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:
43: <p><label for="user_CountryId">Countryid</label><br/>
44: <select name="user_CountryId">
45:    <% @country.each do |country| %>
46:        <option value="< %>"
47:          <%= ' selected' if == @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?
Rich B. (Guest)
on 2006-02-03 02:34
(Received via mailing list)
you want: has_many :users (note  the 's')


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: <select name="user_CountryId">

4034 skippack pike
v. 267.640.2195
f. 215.689.1454
Ben M. (Guest)
on 2006-02-03 15:32
(Received via mailing list)
Do I want that even though I have pluralisation turned off?
This topic is locked and can not be replied to.