4

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?
georgebrock
  • 28,393
  • 13
  • 77
  • 72
Bilal Ahmed
  • 423
  • 1
  • 6
  • 11

1 Answers1

5

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.

Sam Peacey
  • 5,964
  • 1
  • 25
  • 27
  • 2
    In addition to @cheeseweasel answer, one change should be there in LmsUser model the associations should be `belongs_to :user` instead of users and `belongs_to :group_detail` instead of group_details – abhas Sep 14 '12 at 09:05
  • How can i access the data in lms_users table coloum through resultset variable? – Bilal Ahmed Sep 14 '12 at 10:23