ruby on rails - Why would a truthy check on an enum value not work like I expect? -
on membership
model, have following enum value:
enum relative_type: { member: 0, user: 1 }
then elsewhere in code, when comes upon membership
model, have if-branch
if membership.user? member = member.create(first_name: self.first_name, last_name: self.last_name, email: self.email, bio: self.bio, gender: self.gender, avatar: self.avatar, birthday: self.birthday, deceased: false) membership.update!(member: member, invited: nil, relative_type: 0) binding.pry membership.inviter_connection.update!(request_status: 3) end
the weird thing attributes membership.user?
returns false
, execution still gets passed inside if
block.
here example of pry
session.
207: if membership.user? 208: member = member.create(first_name: self.first_name, last_name: self.last_name, email: self.email, bio: self.bio, gender: self.gender, avatar: self.avatar, birthday: self.birthday, deceased: false) 209: membership.update!(member: member, invited: nil, relative_type: 0) => 210: binding.pry 211: membership.inviter_connection.update!(request_status: 3) 212: end 213: end 214: else 215: [1] pry(#<user>)> membership.user? => false [2] pry(#<user>)> membership => #<membership id: 11045, family_tree_id: 76631, user_id: 31643, created_at: "2016-02-06 08:16:54", updated_at: "2016-02-06 08:18:08", relation: "brother", member_id: 9872, invited_id: nil, relative_type: 0> [3] pry(#<user>)> membership.relative_type => "member" [4] pry(#<user>)> membership.member? => true
so membership
object above, can see membership.relative_type
equal 0
, corresponds member
. works expect, particularly given membership.member?
returns true.
what don't understand is....in current instance, why on earth instance of pry being executed within if
block should executed if membership.user?
true....yet false here.
edit 1
the weird thing edited method more explicit, so:
if membership.user? == true
but still sends execution if-block
when membership.user?
false.
207: if membership.user? == true 208: member = member.create(first_name: self.first_name, last_name: self.last_name, email: self.email, bio: self.bio, gender: self.gender, avatar: self.avatar, birthday: self.birthday, deceased: false) 209: membership.update!(member: member, invited: nil, relative_type: 0) => 210: binding.pry 211: membership.inviter_connection.update!(request_status: 3) 212: end 213: end 214: else 215: [1] pry(#<user>)> membership.user? => false [2] pry(#<user>)> membership.user? => false [3] pry(#<user>)> membership.member? => true
edit 2
i took step further , compared actual values of enum, so:
207: if membership.relative_type == 'user' 208: member = member.create(first_name: self.first_name, last_name: self.last_name, email: self.email, bio: self.bio, gender: self.gender, avatar: self.avatar, birthday: self.birthday, deceased: false) 209: membership.update!(member: member, invited: nil, relative_type: 0) => 210: binding.pry 211: membership.inviter_connection.update!(request_status: 3) 212: end 213: end 214: else 215: [1] pry(#<user>)> membership => #<membership id: 11049, family_tree_id: 76639, user_id: 31647, created_at: "2016-02-06 08:41:59", updated_at: "2016-02-06 08:42:02", relation: "brother", member_id: 9874, invited_id: nil, relative_type: 0> [2] pry(#<user>)> membership.relative_type => "member"
but still no dice.
Comments
Post a Comment