【问题标题】:has_many :through, not reloaded/resethas_many :通过,未重新加载/重置
【发布时间】:2016-01-15 18:32:37
【问题描述】:

我有三个模型,其中两个通过连接模型链接:

 # user.rb 
 has_many :user_cards
 has_many :cards, through: :user_cards


 # cards.rb
 has_many :user_cards
 has_many :users, through: user_cards

 # user_cards.rb
 belongs_to :user
 belongs_to :card

当我想给用户添加卡片时,我想指定一个UserCard的属性,这就是我这样创建它的原因:

 # example
 user.user_cards.create(card: card, paid: true)
  • 然后当我做user.cards 它不包含我的新记录。
  • 如果我输入user.user_cards,它确实包含我的新记录。

我需要致电user.cards.reset(或重新加载)。我不喜欢这个解决方案,因为它需要另一个查询。

你知道我怎样才能在没有额外查询的情况下重新加载|刷新|更新user.cards

谢谢!

【问题讨论】:

    标签: ruby-on-rails activerecord has-many-through


    【解决方案1】:

    使用user.cards(true)user.cards(force_reload = true)

    【讨论】:

      猜你喜欢
      • 2013-10-26
      • 1970-01-01
      • 2019-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多