With Rails concerns I can give my model class methods and instance methods through modules by including them. No blog entry or thread that I've found mentions how I can include variables in my model though.
Specifically I would like to give my including model a class instance variable @question
, but I don't know where to put the declaration in the module so it is applied. I would also like the class instance variable to be overridden if the model itself declares that variable.
Does the ActiveSupport::Concern
module actually care about variables at all?
module ContentAttribute
extend ActiveSupport::Concern
def foo
p "hi"
end
module ClassMethods
# @question = "I am a generic question." [doesn't work]
def bar
p "yo"
end
end
end
class Video < ActiveRecord::Base
include ContentAttribute
# @question = "Specific question"; [should override the generic question]
end
included
block and put@question = ...
inside? – Taraxacumincluded do; @question = "This is a generic question."; end
? No, I still get aNoMethodError
onVideo.question
(I defined anattr_accessor :question
in the video model.) – Frizzattr_accessor :question
in the included block – Taraxacum