ActiveRecord :: HasManyThroughOrderError:不能具有has_many:through关联

丹尼尔·潘欣(Danil Panshin)

在我的Rails应用程序中,我正在尝试创建一个系统,该系统将向用户奖励各种成就的徽章

创建了一个表“ user_badges”

移民:

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

型号UserBadge:

class UserBadge < ApplicationRecord

  belongs_to :user
  belongs_to :badge

end

徽章型号:

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

模型用户:

class User < ApplicationRecord
  ...

  has_many :badges, through: :user_badges
  has_many :user_badges

  ...
end

当我尝试向用户添加徽章时:

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

User.last.badges << b

我收到此错误:

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

当我简单地打电话给:

User.last.badges

同样的错误:

ActiveRecord::HasManyThroughOrderError: Cannot have a has_many 
:through association 'User#badges' which goes through 
'User#user_badges' before the through association is defined.
维沙尔泰姬陵

has_many首先定义关联,然后添加through:关联

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

具有has_many关联的Ruby ActiveRecord查询

来自分类Dev

Rails,ActiveRecord,has_many:through:查找没有关联的所有记录

来自分类Dev

ActiveRecord如何在Rails中通过has_many:through关系将现有记录添加到关联中?

来自分类Dev

ActiveRecord :: HasManyThroughOrderError

来自分类Dev

ActiveRecord:has_many,包括

来自分类Dev

ActiveRecord has_many :through 不通过赋值

来自分类Dev

使用自定义ActiveRecord :: Type(二进制)列时,“ has_many through”关联不起作用

来自分类Dev

Rails:在定义关联之前不能具有has_many:through关联

来自分类Dev

ActiveRecord关联

来自分类Dev

ActiveRecord关联

来自分类Dev

Rails ActiveRecord has_many问题

来自分类Dev

Rails / ActiveRecord has_many通过:未保存对象上的关联

来自分类Dev

ActiveRecord查询,按关联顺序排序,has_many的最后一个

来自分类Dev

ActiveRecord has_many关联NameError:未初始化的常量

来自分类Dev

对于新的ActiveRecord模型,为什么某些has_many:through关联向SQL查询添加(1 = 0)谓词和distinct子句?

来自分类Dev

Rails ActiveRecord 等价于 Laravel ORM `attach()` 方法,用于多态 has_many :through

来自分类Dev

通过关系在has_many上的ActiveRecord :: InvalidForeignKey

来自分类Dev

ActiveRecord:按 has_many 的计数分组:通过模型

来自分类Dev

ActiveRecord has_many通过两个模型

来自分类Dev

在has_many中通过关联使用class_name时,Rails自定义ActiveRecord :: Type失败

来自分类Dev

如何使用ActiveRecord在没有桥表的情况下创建has_many关系

来自分类Dev

存根ActiveRecord关联

来自分类Dev

Rails ActiveRecord关联

来自分类Dev

ActiveRecord双重关联

来自分类Dev

ActiveRecord有很多通过

来自分类Dev

Rails Activerecord:如何排除具有多个关联记录的记录

来自分类Dev

如何选择具有关联对象的ActiveRecord对象?

来自分类Dev

验证ActiveRecord模型具有相同的关联/组

来自分类Dev

具有两个不同模型的ActiveRecord关联

Related 相关文章

  1. 1

    具有has_many关联的Ruby ActiveRecord查询

  2. 2

    Rails,ActiveRecord,has_many:through:查找没有关联的所有记录

  3. 3

    ActiveRecord如何在Rails中通过has_many:through关系将现有记录添加到关联中?

  4. 4

    ActiveRecord :: HasManyThroughOrderError

  5. 5

    ActiveRecord:has_many,包括

  6. 6

    ActiveRecord has_many :through 不通过赋值

  7. 7

    使用自定义ActiveRecord :: Type(二进制)列时,“ has_many through”关联不起作用

  8. 8

    Rails:在定义关联之前不能具有has_many:through关联

  9. 9

    ActiveRecord关联

  10. 10

    ActiveRecord关联

  11. 11

    Rails ActiveRecord has_many问题

  12. 12

    Rails / ActiveRecord has_many通过:未保存对象上的关联

  13. 13

    ActiveRecord查询,按关联顺序排序,has_many的最后一个

  14. 14

    ActiveRecord has_many关联NameError:未初始化的常量

  15. 15

    对于新的ActiveRecord模型,为什么某些has_many:through关联向SQL查询添加(1 = 0)谓词和distinct子句?

  16. 16

    Rails ActiveRecord 等价于 Laravel ORM `attach()` 方法,用于多态 has_many :through

  17. 17

    通过关系在has_many上的ActiveRecord :: InvalidForeignKey

  18. 18

    ActiveRecord:按 has_many 的计数分组:通过模型

  19. 19

    ActiveRecord has_many通过两个模型

  20. 20

    在has_many中通过关联使用class_name时,Rails自定义ActiveRecord :: Type失败

  21. 21

    如何使用ActiveRecord在没有桥表的情况下创建has_many关系

  22. 22

    存根ActiveRecord关联

  23. 23

    Rails ActiveRecord关联

  24. 24

    ActiveRecord双重关联

  25. 25

    ActiveRecord有很多通过

  26. 26

    Rails Activerecord:如何排除具有多个关联记录的记录

  27. 27

    如何选择具有关联对象的ActiveRecord对象?

  28. 28

    验证ActiveRecord模型具有相同的关联/组

  29. 29

    具有两个不同模型的ActiveRecord关联

热门标签

归档