【问题标题】:rails counter cache and foreign key testrails 计数器缓存和外键测试
【发布时间】:2013-04-08 14:53:08
【问题描述】:

这是我最喜欢的设计师模特

favorite_designer.rb

class FavoriteDesigner < ActiveRecord::Base
  belongs_to :user, :counter_cache => true
  belongs_to :designer, :class_name => "User", :foreign_key => :designer_id

  validates_presence_of :user_id
  validates_presence_of :designer_id

  validates_numericality_of :user_id, :unless => Proc.new{|f| f.user_id.blank?}
  validates_numericality_of :designer_id, :unless => Proc.new{|f| f.designer_id.blank?}
end

如何使用 shoulda 在 rspec 中测试 :counter_cache =&gt; true and :foreign_key =&gt; :designer_id

【问题讨论】:

    标签: ruby-on-rails rspec shoulda


    【解决方案1】:

    您可以使用 counter_cachewith_foreign_key 应该匹配器:

    it { should belong_to(:organization).counter_cache(true) }
    
    it { should have_many(:worries).with_foreign_key('worrier_id') }
    

    有关完整文档和示例,请查看:

    Counter cache doc

    Foreign key doc

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-26
      • 1970-01-01
      • 2019-01-09
      • 2013-05-04
      • 2014-09-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多