Rails find with a block

前端 未结 2 584
天命终不由人
天命终不由人 2020-12-20 16:21

I have seen Rails find method taking a block as

Consumer.find do |c|
  c.id == 3
end

Which is similar to Consumer.find(3).

2条回答
  •  Happy的楠姐
    2020-12-20 16:58

    The result may be similar, but the SQL query is not similar to Consumer.find(3)

    It is fetching all the consumers and then filtering based on the block. I cant think of a use case where this might be useful

    Here is a sample query in the console

     consumer = Consumer.find {|c|c.id == 2}
     # Consumer Load (0.3ms)  SELECT `consumers`.* FROM `consumers`
     # => #
    

提交回复
热议问题