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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9757acb5b356cbf87576c2005f2cf33c?d=identicon&s=25 Ben Moxon (Guest)
on 2006-02-03 01: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"
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?
786aca6b4728622a57a2c34fb1c9f0cd?d=identicon&s=25 Rich Brant (Guest)
on 2006-02-03 01:34
(Received via mailing list)
you want: has_many :users (note  the 's')

hth,
rich

On 2/2/06, Ben Moxon <glenatron@gmail.com> 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">
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
http://brantinteractive.com
rbrant@brantinteractive.com
4034 skippack pike
v. 267.640.2195
f. 215.689.1454
9757acb5b356cbf87576c2005f2cf33c?d=identicon&s=25 Ben Moxon (Guest)
on 2006-02-03 14: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.