为什么对枚举值的真实检查不能像我期望的那样工作?

时间:2016-02-06 08:25:49

标签: ruby-on-rails ruby ruby-on-rails-4 enums truthiness

在我的membership模型上,我有以下枚举值:

enum relative_type: { member: 0, user: 1 }

然后在某些代码的其他地方,当它出现在membership模型时,我有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

奇怪的是membership.user?返回false的属性,执行仍然在if块内传递。

以下是a pry会话的示例。

    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

因此,从上面的membership对象中,您可以看到membership.relative_type等于0,与member相对应。这样就像你期望的那样工作,特别是考虑到membership.member?返回true。

我不明白的是....在这个当前实例中,为什么PRY的这个实例是在if块内执行的,只有membership.user?才能执行这是真的....但在这里显然是假的。

修改1

奇怪的是我编辑的方法更加明确,如下所示:

if membership.user? == true

但即使if-block为假,它仍会将执行发送到membership.user?

    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

修改2

我甚至更进一步,比较了枚举的实际值,如下:

    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"

但仍然没有骰子。

0 个答案:

没有答案