2

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

最喜欢的设计师.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

如何:counter_cache => true and :foreign_key => :designer_id使用 shoulda 在 rspec 中进行测试?

4

1 回答 1

1

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

it { should belong_to(:organization).counter_cache(true) }

it { should have_many(:worries).with_foreign_key('worrier_id') }

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

计数器缓存文档

外键文档

于 2015-12-25T12:16:46.310 回答