Search code examples
ruby-on-railsruby-on-rails-3activerecordactivesupport-concern

ActiveRecord Concern spec fails when uses self.class


I have next Siteable concern:

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

Example model with this concern:

class IndustryLink < LinkResource
  require 'concerns/siteable.rb'
  include Siteable

  belongs_to :author, :class_name => 'User'
  belongs_to :industry
  validates_presence_of :name, :link
end

It work fine on the server. But all specs with this model fails with similar errors:

 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)>'

So, obviously that self.class is not Industry in this case and I don't know how to fix this.

If I move for_site to model and change self.class to Industry specs passes.

Checked for ruby 1.9.3, 2.1.1, Rails 3.2.19


Solution

  • Within your lambda self is already the model class - Industry, so the self.class is Class

    You should just be using by_site (possibly unscoping it first, depending on what you need)