I am using Ruby 1.9.2 and Ruby on Rails 3.2.2. I have following statements:
class Category < ActiveRecord::Base
include Commentable
acts_as_list :scope => 'category_comment_id'
has_many :comments, :class_name => 'CategoryComment'
# ...
end
module Commentable
extend ActiveSupport::Concern
included do
acts_as_list :scope => 'comment_id'
has_many :comments, :class_name => 'Comment'
# Other useful method statements...
end
# Other useful method statements...
end
In the above code I am trying to override both acts_as_something and has_many methods added to the Category class by the including Commentable module. Both methods are stated “in the scope of” Category so the above code does not work as expected: methods are not overrode.
Is it possible to override those methods? If so, how?
You should include your module in the end of your class definition. As it is now, methods from the module get injected before the class defines its method. This is so because ruby processes and evaluates code in the top-down manner. Therefore, a little bit later it encounters class’ own definitions of the methods and overwrites those that came from the module.
So, use this knowledge according to your intention: who should override whom. If methods from the module should take dominance over those in the class, include it in the end.
Edit
Given this code
Then
And