ruby on rails - Define another attribute in setter -
i want define role attribute in user model when set physician attribute, know attribute defined user instance has role of :physician (as enum).
it works role defined, role wrongly assigned enum type shown in rspec test
enum role: [:assistant, :physician, :patient] def physician=(value) write_attribute(:role, "physician") super(value) end
rspec:
user physician should physician failure/error: expect(user.role).to eq('physician') expected: "physician" got: "assistant" (compared using ==) # ./spec/models/user_spec
in fact tried replicate manually in rails console
user = user.new physician = physician.new user.physician = physician user.physician? => false user.role => "assistant"
i suspect write_attribute not work nicely enums...
p.d. tried write_attribute(:role, :physician) sets role value nil
i think it's fixed with:
def physician=(value) self.send("role=", "physician") super(value) end
but i'm not sure if it's idea call send directly this
Comments
Post a Comment