Topic: Totally stuck with 2 has_many/belongs_to associations

I have an application with 3 models


class Order < ActiveRecord::Base
    has_many :events
    has_many :users
    ...
end

class Event < ActiveRecord::Base
    belongs_to :order
    ...
end

class User < ActiveRecord::Base
    belongs_to :order
    ...
end


Both, the events and the users table have an order_id column as foreign key, both tables are populated with data which should give the results one should expect from this simple model.

Order.find(1).events gives me all events for the order with id 1. Works as advertised ;-)

Order.find(1).users gives me the following error

NoMethodError: undefined method `users' for #<Order:0x33d6ab4>
        from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.1/lib/active_record/base.rb:1848:in `method_missing'
        from (irb):109

I'm totally stuck, has anybody an idea how to solve this problem.

Any help is appreciated

Thanks in advance

Thomas

Last edited by DocTom (2007-01-29 18:34:16)

Re: Totally stuck with 2 has_many/belongs_to associations

Check the Order model again, are you certain you set "has_many :users" and not "has_many :user" or something?

Also, looks like you're running this command from script/console, make sure to exit out of the console and run it again after every change to the model because it won't automatically pick up any change (say if you added "has_many :users" after starting script/console)

Railscasts - Free Ruby on Rails Screencasts

Re: Totally stuck with 2 has_many/belongs_to associations

Thank you very much Ryan!

My models were o.k., a restart of the console solved the problem.

Thanks again

Thomas