Search code examples
ruby-on-railsrubyactiverecordassociations

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.

Solution

  • 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