Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

update_attribute is annoying, what about a bang method?

#6609

I'm tired of writing update_attribute. Hope it's not just me. I propose having a bang method in attributes:

@foo = Foo.find(10) @foo.bar! 'baz' // will update bar to baz

Reported by Juan Maiz (aka João Milho or John Corn) · March 23rd, 2011 @ 02:17 PM

State: wontfix
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Pedro Axelrud
    Pedro Axelrud

    I liked the idea, very nice.

    March 23rd, 2011 @ 08:02 PM

  2. Rafael Souza
    Rafael Souza

    +1, sounds cool for me

    update_attribute sometimes confuses me, I often try to pass a hash for it, mainly when I write some update_attributeS before :)

    March 23rd, 2011 @ 08:35 PM

  3. Juan Maiz (aka João Milho or John Corn)
    Juan Maiz (aka João Milho or John Corn)

    The implementation also seemed very natural.

    Read:
    @foo.bar

    Write:
    @foo.bar=

    Query:
    @foo.bar?

    ... ergo Update: @foo.bar!

    ... looks like the project was asking for an Update module :D

    March 23rd, 2011 @ 08:36 PM

  4. Rodrigo Urubatan
    Rodrigo Urubatan

    +1 sounds natural and it is less code to write.

    March 24th, 2011 @ 12:49 PM

  5. Robson Mendonça
    Robson Mendonça

    +1 that's a good feature, much more ruby like.

    March 24th, 2011 @ 01:50 PM

  6. Nícolas Iensen
    Nícolas Iensen

    +1 less code and very intuitive

    March 24th, 2011 @ 03:51 PM

  7. Corey Ward
    Corey Ward

    -1

    Dropping = is actually counter-intuitive and not very Ruby-like. It's even less Rails-like.

    It's pretty easy to use:

    @foo.bar = 'baz' and @foo.save
    

    Or if you prefer not to have an and (and I can't blame you):

    @foo.set(:bar, 'baz')
    

    Seems pretty straightforward and much shorter, plus it's just an alias for update_attribute.

    March 24th, 2011 @ 05:39 PM

  8. Andrew White
    Andrew White
    • State changed from new to wontfix
    • Importance changed from to Low

    The consensus seems to be that it seems more like a shortcut for updates_attributes!, e.g:

      def foo!(value)
        update_attributes!(:foo => value)
      end
    

    Thanks for the suggestion though.

    March 24th, 2011 @ 07:45 PM