0

I'm getting an error when trying to call a concern method within a models method. I have several concerns and I've set them up in the same way. The strange thing is that one method works but the other one doesn't. Here is my code.

param_set.rb

class ParamSet < ApplicationRecord

include Searchable
include Comparable

def self.import(file, user_id, group_id = 1, project_id = nil)
    param_set_ids_map = {}
    yml = YAML.load_file(file)
    yml.each do |component, elements|
      case component
      when 'param_sets'
        elements.each do |importing_attributes|
          old_param_set_id = importing_attributes.delete('id')
          importing_attributes['user_id'] = user_id
          importing_attributes['group_id'] = group_id
          importing_attributes['project_id'] = project_id

          ParamSet.search(User.first, {search: "test"})
          ParamSet.compare(importing_attributes)

        end
      end
    end
    param_set_ids_map
  end
 end

comparable.rb

module Comparable
  extend ActiveSupport::Concern

  module ClassMethods
    def compare(importing_attributes)
      logger.debug "Works!"
    end
  end
end

searchable.rb

module Searchable
  extend ActiveSupport::Concern

  module ClassMethods
    def search(current_user, params, visible: true)
      results = paged_filter(current_user, params[:scope], params[:page] || 1)
      results = results.visible if visible
      results.where!("lower(name) LIKE lower(?) OR lower(description) LIKE lower(?)", "%#{params[:search]}%", "%#{params[:search]}%")
      if params[:order].blank?
        results.order!('updated_at DESC')
      else
        results.order!(params[:order])
      end
      results.reverse_order! if params[:reverse] == 'true'
      results || []
    end
  end
end

The error that I get is:

undefined method `compare' for #<Class:0x000056167cb01718>

The method ParamSet.search(User.first, {search: "test"}) works find and doesn't give an error. The method ParamSet.compare(importing_attributes) however gives the error. I don't know what is going on and what the difference is between calling the two concern methods within a model method.

Can anybody explain what is going on?

2
  • 1
    try to change method name compare . Commented Jul 3, 2018 at 8:41
  • Comparable is already module of ruby. ruby-doc.org/core-2.2.3/Comparable.html Commented Jul 3, 2018 at 8:43

1 Answer 1

3

When you do

include Comparable

you're including the Comparable module of ruby, not your own module. That module doesn't have a compare method.

Try changing your module name...

module Matchable

and the file to matchable.rb

and then

include Matchable

And you should be ok.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.