1

我有下一个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.classIndustry规格通过。

检查 ruby​​ 1.9.3、2.1.1、Rails 3.2.19

4

1 回答 1

1

在您的 lambda self 中已经是模型类 - Industry,所以self.classClass

您应该只是使用by_site(可能首先取消范围,具体取决于您的需要)

于 2014-09-05T07:25:58.473 回答