Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveRecord::HasManyThroughOrderError: Cannot have a has_many :through association

In my rails app I'm trying to create a system that will reward users with badges for various achievements

created a table 'user_badges'

migration:

class CreateUserBadges < ActiveRecord::Migration[5.1]
  def change
    create_table :user_badges do |t|

    t.references :user, foreign_key: true
    t.references :badge, foreign_key: true

    t.timestamps
    end
  end
end

model UserBadge:

class UserBadge < ApplicationRecord

  belongs_to :user
  belongs_to :badge

end

модель Badge:

class Badge < ApplicationRecord
  has_many :users, through: :user_badges
  has_many :user_badges
end

model User:

class User < ApplicationRecord
  ...

  has_many :badges, through: :user_badges
  has_many :user_badges

  ...
end

when I try to add a badge to the user:

b = Badge.create(title: 'first')

User.last.badges << b

I get this error:

ActiveRecord::HasManyThroughOrderError: Cannot have a has_many 
:through association 'User#badges' which goes through 
'User#user_badges' before the through association is defined.

also when I simply call:

User.last.badges

same error:

ActiveRecord::HasManyThroughOrderError: Cannot have a has_many 
:through association 'User#badges' which goes through 
'User#user_badges' before the through association is defined.
like image 216
Danil Panshin Avatar asked Mar 23 '18 13:03

Danil Panshin


2 Answers

Define has_many association first then add through: association

class UserBadge < ApplicationRecord
  belongs_to :user
  belongs_to :badge
end

class Badge < ApplicationRecord
  has_many :user_badges # has_many association comes first
  has_many :users, through: :user_badges #through association comes after
end


class User < ApplicationRecord
  ...
  has_many :user_badges
  has_many :badges, through: :user_badges
  ...
end
like image 200
Vishal Taj PM Avatar answered Nov 11 '22 10:11

Vishal Taj PM


Note, in case you mistakenly wrote first has_many 2 times, then it can reproduce this error too. E.g.

class User < ApplicationRecord
  ...
  has_many :user_badges
  has_many :badges, through: :user_badges
  ...
  has_many :user_badges
end

# => Leads to the error of ActiveRecord::HasManyThroughOrderError: Cannot have a has_many :through association 'User#badges' which goes through 'User#user_badges' before the through association is defined.

Active Record should alert has_many being used two times IMHO...

like image 35
Yuki Inoue Avatar answered Nov 11 '22 08:11

Yuki Inoue