我有下一个Siteable
担心:
module Siteable
extend ActiveSupport::Concern
included do
belongs_to :site
scope :by_site, lambda { |site| where(site_id: site.try(:id)) }
scope :for_site, lambda { |site| by_site self.class.by_site(site).any? ? site : nil }
end
end
有此问题的示例模型:
class IndustryLink < LinkResource
require 'concerns/siteable.rb'
include Siteable
belongs_to :author, :class_name => 'User'
belongs_to :industry
validates_presence_of :name, :link
end
它在服务器上工作正常。但是这个模型的所有规格都失败了,出现了类似的错误:
Failure/Error: industry = Factory(:industry, :name => 'new industry')
NoMethodError:
undefined method `by_site' for Class:Class
# ./app/models/concerns/siteable.rb:8:in `block (2 levels) in <module:Siteable>'
# ./app/models/industry_link.rb:12:in `get_objects'
# ./app/models/concerns/reorderable.rb:47:in `add_number'
# ./spec/views/sitemap/show.xml.builder_spec.rb:41:in `block (2 levels) in <top (required)>'
所以,显然这self.class
不是Industry
这种情况,我不知道如何解决这个问题。
如果我转向for_site
模型并更改self.class
为Industry
规格通过。
检查 ruby 1.9.3、2.1.1、Rails 3.2.19