I'm trying to override the initialize method. See below
class Restriction < ActiveRecord::Base
RESTRICTION_TYPES = {
less_than: "IND<X",
greater_than: "X<IND",
between: "X<IND<Y"
}
def initialize restriction_type_name
super
formula = RESTRICTION_TYPES[restriction_type_name]
end
private
def formula=f
self[:formula] = f
end
end
When I run r = Restriction.new(:between)
I get the exception:
NoMethodError: undefined method `stringify_keys' for :between:Symbol
What am I doing wrong?
BTW I'm doing this due to formula
attribute can't be acceded from outside.
You can't override initialize
, and what you have wouldnt work anyway (it would try to set a local variable called formula
in the initialize method). You can, however, move the conditions into your formula=
setter, so it reads like:
def formula=(restriction_type_name)
self[:formula] = RESTRICTION_TYPES[restriction_type_name]
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.