Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7041751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:05:02+00:00 2026-05-28T02:05:02+00:00

I have a model where I have Users, Courses, and I connect the two

  • 0

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

Users

class User < ActiveRecord::Base
  has_many :roles
  has_many :courses, :through => :roles
end

Roles

class Role < ActiveRecord::Base
  belongs_to :user
  belongs_to :course
end

Courses

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

Error

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T02:05:03+00:00Added an answer on May 28, 2026 at 2:05 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Organization model that has_many users through affiliations. And, in the form
Say that I have two models- Users and Accounts. Each account can have at
I have a site with users who can take terms at University: class Term(models.Model):
I have a model called company that has_many users then users belongs_to company. class
For instance, let's say I have a User model. Users have things like logins,
User is a model. I have 100 users. I need to present the list
Say that you have an application where different kind of users can sign: Firms,
I have models User and Message. Every user can send messages to other users.
I have three tables: users, courses and documents. I would like to upload documents
I have following problem: have to model classes (posts and users). Main view is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.