简体   繁体   中英

rails has_many and has_many :through error

I have this code in family_service model:

has_many :families_users
has_many :users, :through => :families_users

But when trying to execute in a loop (service is a family_service)

users << service.users

I get this error

ActiveRecord::HasManyThroughOrderError in Budgets#index

Cannot have a has_many :through association 'FamilyService#users'
which goes through 'FamilyService#families_users' before the through association is defined.

But as you can see, it is defined as it should be.

After a big effort, I found that I have this line before the lines in the question:

has_and_belongs_to_many :users, :join_table => :families_users

So this line actually uses a: join_table which is equal to :through before the actual line of has_many:families_users .

That's why I got the error.

Hopefully someone that is upgrading to rails 5 will find this handy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM