Undefined method for a belongs_to association
Posted
by
marimaf
on Stack Overflow
See other posts from Stack Overflow
or by marimaf
Published on 2012-06-04T04:36:39Z
Indexed on
2012/06/04
4:40 UTC
Read the original article
Hit count: 207
My code:
class User < ActiveRecord::Base
belongs_to :university
end
class University < ActiveRecord::Base
has_many :users, dependent: :destroy
end
and my model User has a university_id attribute.
If I do University.find(1).users
I get the list of users, but if I do User.find(1).university
(and I checked that university_id is not nil here) I get:
NoMethodError: undefined method `university' for
from /home/mari/.rvm/gems/ruby-1.9.2-p290/gems/activemodel-3.0.10/lib/active_model/attribute_methods.rb :392:in `method_missing' from /home/mari/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.10/lib/active_record/attribute_methods. rb:46:in `method_missing' from (irb):14 from /home/mari/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.10/lib/rails/commands/console.rb:44:in
sta rt' from /home/mari/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.10/lib/rails/commands/console.rb:8:in
star t' from /home/mari/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.10/lib/rails/commands.rb:23:in<top (requi red)>' from script/rails:6:in
require' from script/rails:6:in `'
What am I doing wrong? I have another models and they are working just fine. Any suggestions? Thanks in advance
© Stack Overflow or respective owner