class Project
include Listable
listtable(data_attribute: :verified_at)
end
module Listable
class_methods do
def listable(data_attribute: :verified_at)
raise ActiveModel::MissingAttributeError,
"Must have a verified_at attribute" unless respond_to?(:verified_at)
end
end
end
In my
project model i am having column verified_at
. if i am not having verified_at column in my table it should raise the error.
But here it is not responding properly. always raising the error(even the verified_at is present also)
Expected
Wherever i am including this concern in my model, it should check the verified_at column is present or not. If it is not present, should raise the error.
It is not happening for me, Please suggest me any solution, Thanks in advance
The problem is that the model doesn't respond_to?
in the way you expected it to do.
Project.respond_to?(:id)
# false
why? because you are asking the class itself if it has the id
attribute, that only works on an instance.
Project.first.respond_to?(:id)
# true
To work around this you can take advantage of the Project.column_names
method, like this.
raise ActiveModel::MissingAttributeError,
"Must have a verified_at attribute" unless column_names.include?('verified_at')