Reputation: 15828
I have a model where I have Users, Courses, and I connect the two through a Roles model. Everything seems to work well, i can assign and retrieve students, i can retrieve a teacher yet when I assign a teacher to a course. I get an error
Course.last.teacher
# => <# User username: 'schneems' ...>
course = Course.new
course.students = User.last(3)
# => [<# User ... >, <# User ... >, <# User ... >]
course.teacher = User.first
# => NoMethodError: undefined method 'update_attributes' for #<Class:0x007f86b29ee838>
Here are my basic models
class User < ActiveRecord::Base
has_many :roles
has_many :courses, :through => :roles
end
class Role < ActiveRecord::Base
belongs_to :user
belongs_to :course
end
class Course < ActiveRecord::Base
has_many :roles
has_one :teacher, :through => :roles, :source => :user, :conditions => ['roles.name = ?', 'teacher']
has_many :students, :through => :roles, :source => :user, :conditions => ['roles.name = ?', 'student']
end
The full stack trace looks like this:
NoMethodError: undefined method `update_attributes' for #<Class:0x007f86b29ee838>
from /Users/schneems/.rvm/gems/ruby-1.9.2-p290@hourschool/gems/activerecord-3.0.9/lib/active_record/base.rb:1014:in `method_missing'
from /Users/schneems/.rvm/gems/ruby-1.9.2-p290@hourschool/gems/activerecord-3.0.9/lib/active_record/associations/association_collection.rb:444:in `block in method_missing'
from /Users/schneems/.rvm/gems/ruby-1.9.2-p290@hourschool/gems/activerecord-3.0.9/lib/active_record/base.rb:1127:in `with_scope'
from /Users/schneems/.rvm/gems/ruby-1.9.2-p290@hourschool/gems/activerecord-3.0.9/lib/active_record/associations/association_proxy.rb:207:in `with_scope'
from /Users/schneems/.rvm/gems/ruby-1.9.2-p290@hourschool/gems/activerecord-3.0.9/lib/active_record/associations/association_collection.rb:440:in `method_missing'
from /Users/schneems/.rvm/gems/ruby-1.9.2-p290@hourschool/gems/activerecord-3.0.9/lib/active_record/associations/has_one_through_association.rb:22:in `create_through_record'
from /Users/schneems/.rvm/gems/ruby-1.9.2-p290@hourschool/gems/activerecord-3.0.9/lib/active_record/associations/has_one_through_association.rb:10:in `replace'
from /Users/schneems/.rvm/gems/ruby-1.9.2-p290@hourschool/gems/activerecord-3.0.9/lib/active_record/associations.rb:1465:in `block in association_accessor_methods'
from (irb):43
from /Users/schneems/.rvm/gems/ruby-1.9.2-p290@hourschool/gems/railties-3.0.9/lib/rails/commands/console.rb:44:in `start'
from /Users/schneems/.rvm/gems/ruby-1.9.2-p290@hourschool/gems/railties-3.0.9/lib/rails/commands/console.rb:8:in `start'
from /Users/schneems/.rvm/gems/ruby-1.9.2-p290@hourschool/gems/railties-3.0.9/lib/rails/commands.rb:23:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
Does anyone know how I can get this association to work where I can retrieve and set the teacher attributes of a course as expected? Using Rails 3.0.9
Upvotes: 1
Views: 1204
Reputation: 23648
If you run this exact code on Rails 3.1 you will get this error message:
ActiveRecord::HasOneThroughCantAssociateThroughCollection: Cannot have a has_one :through association 'Course#teacher' where the :through association 'Course#roles' is a collection. Specify a has_one or belongs_to association in the :through option instead.
The right association to use here would be belongs_to
but in that case you can't put that into the roles table.
greetings Daniel
Upvotes: 2
Reputation: 25757
Try adding :class_name => "User"
to the :teacher association on Course
.
Upvotes: 0