I find myself writing this kind of pattern a lot. It's really ugly and annoying especially if I'm just running a quick change in Console.
u = User.find(some_id)
u.name = "new name"
u.save
What's the best way to make it so that I can do this in a single statement. I want to be able to do something like this instead.
User.find(some_id).set({name:"new nane"}).save
find_and_update_byas a method on ActiveRecord. Rails hasfind_or_create_bywhich makes sense cause if the record is not found, you want to create it with given attributes. - AbhinayUser.find(..)gives you more control over the code flow. you may choose to not execute the rest of the code and exit immediately if the record is not found. whereas in case of something likefind_and_update_by(method not present) you are forced to run update even when the record is not present. - AbhinayUser.find(some_id).update(name: 'new name', other_attribute: 'abc')- Amit Patel