What is the difference between using .exists?, and .present? in Ruby?
我想确保我在正确的场合使用它们,并想知道任何细微之处。 它们似乎以相同的方式起作用,即检查是否已经定义了一个对象字段,当我通过控制台使用它们时,当我进行谷歌搜索时,并没有在线提供大量信息。 谢谢!
澄清一下:
当下?
An object is
present if it’s notblank? . An object isblank if it’sfalse , empty, or a whitespace string.
所以,例如:
1 2 | ["","", false, nil, [], {} ].any?(&:present?) # => false |
存在?
Asserts the existence of a resource, returning true if the resource is found.
1 2 | Note.create(:title => 'Hello, world.', :body => 'Nothing more for now...') Note.exists?(1) # => true |
两种方法之间的最大区别在于,当您调用
为了表明这一点,我在User上添加了after_initialize。它打印出来:'你已经初始化了一个对象!'
User.where(name: 'mike').present?
1 2 3 | User Load (8.1ms) SELECT"users".* FROM"users" WHERE"users"."name" = $1 ORDER BY users.id ASC [["name", 'mike']] You have initialized an object! You have initialized an object! |
User.exists?(name: 'mike')
1 | User Exists (2.4ms) SELECT 1 AS one FROM"users" WHERE"users"."name" = $1 ORDER BY users.id ASC LIMIT 1 [["name", 'mike']] |
性能存在巨大差异,
本文基准
简而言之,
两者生成的SQL也不同。
1 2 | Thing.where(name:"Bob").present? # => SELECT COUNT(*) FROM things WHERE things.name ="Bob"; |
1 2 | Thing.exists?(name:"Bob") # => SELECT 1 AS one from things WHERE name ="Bob" limit 1; |
它们似乎都以相同的速度运行,但根据您的情况可能会有所不同。
您可以使用
1 2 3 4 5 6 7 | all_endorsements_11 = ArtworkEndorsement.where(user_id: 11) ArtworkEndorsement Load (0.3ms) SELECT"artwork_endorsements".* FROM"artwork_endorsements" WHERE"artwork_endorsements"."user_id" = $1 [["user_id", 11]] all_endorsements_11.present? => true all_endorsements_11.exists? ArtworkEndorsement Exists (0.4ms) SELECT 1 AS one FROM"artwork_endorsements" WHERE"artwork_endorsements"."user_id" = $1 LIMIT 1 [["user_id", 11]] => true |