DEV Community

Discussion on: Finding an Initially Confusing Result in Rails

Collapse
 
arni1981 profile image
Árni Gunnarsson

How about using tap?
As in

bob = Friend.find_or_initialize_by(first_name: "Bob").tap do |friend|
  friend.last_name = "Jones"
end
Enter fullscreen mode Exit fullscreen mode
Collapse
 
kevin_j_m profile image
Kevin Murphy

Using tap will work as well. If you care about the return value, and you want it to be the friend without having to explicitly adding a new line to return the friend, it's a great choice.

I have a similar post on blocks in AR methods that also touches on tap available here: dev.to/thegnarco/activerecord-s-ne...