Association named was not found

Bilal Ahmed picture Bilal Ahmed · Sep 14, 2012 · Viewed 7.2k times · Source

I am working in Rails 2. I have three tables: users, lms_users and group_details.

In lms_users id from users and group_details is coming as foreign keys. lms_users has its own attributes as well. I am unable to define association in their respective models. I tried this:

In the LmsUser model

belongs_to :users
belongs_to :group_details

In the User model

has_many :group_details , :through => :lms_users

In the GroupDetail model

has_many :users , :through => :lms_users

But I am getting this error

ActiveRecord::ConfigurationError in Lms usersController#index
Association named 'lms_user' was not found; perhaps you misspelled it?

Answer

Sam Peacey picture Sam Peacey · Sep 14, 2012

You need to add the association you're going through as a has_many.

So for example, your user.rb should look like this:

has_many :lms_users
has_many :group_details , :through => :lms_users

And your group_detail.rb should contain the following:

has_many :lms_users
has_many :users , :through => :lms_users

:through goes through an association, so the association needs to already be set up.