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.
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
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With