Inspection on attr_accessor in a Rails model?
Perhaps this is another it's-just-me incident.
I have a Rails model thus:
class JobPost < ActiveRecord::Base
# attributes
attr_accessible .... # attributes that are columns in database
attr_accessor :organization_name # this is a "virtual attribute"
# Rest omitted for brevity
end
Pretty standard stuff. But RM 3.2.4 flags this attr_accessor (:organization_name) as unresolved:
This inspection warns about the references in Ruby code which can't be resolved to any valid target.
The IDE highlights all the symbols in the code which cannot be found anywhere within your project and most likely they should be treated as incorrect ones.
This most likely indicates a typo or some other error which may occur at runtime.
I don't understand why it does so. This attr_accessor method says that the accessor methods for this attribute are provided by default. Then why does inspection think that it's an unreferenced variable?
(Don't tell me that I can always disable this inspection, I don't want to).
Further, I press on. I do Alt+Enter on the symbol (:organization_name) that RM thinks is unresolvable and it offers me to create the method. I yield, ask it go ahead. And it creates this method:
def self.organization_name
# code here
end
Whoa!
It creates a class-instance method (or the so-called class method) and not the instance method which is what is should.
The implications of this are subtle. The @organization_name will now refer to completely different variable and not the organization_name of an instance of JobPost model.
Bug(s)?
Regards,
Kedar
Please sign in to leave a comment.
Are you experiencing same problem as here?