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?