나는 다음 Siteable
문제가이 문제에액티브 관심사 사양이 실패 할 때 사용 self.class
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
으로 변경합니다. 루비 1.9.3, 2.1.1에 대한 검사
, 당신의 람다 자체 내에서 3.2.19
모듈을 포함하면 해당 메소드를 객체 인스턴스에서 사용할 수 있습니다. 모듈을 포함시키지 않고 모듈을 확장하려고 시도 했습니까? 그런 다음 클래스 메서드로 모듈의 메서드를 추가합니다. –
이것에 대해서는 잊어 버리십시오, 지금 시도하겠습니다. – zishe
작동하지 않습니다. 관심사가''ActiveSupport :: Concern'을 확장하고''include''를 우려 할 때 실제로 적절한 방법입니다. – zishe