What is the shortest way in ruby to write these four instructions?Rails: update_attribute vs update_attributesWhat is the easiest way to duplicate an activerecord record?What is the “right” way to iterate through an array in Ruby?What is the difference between include and require in Ruby?How to write a switch statement in RubyWhat does Ruby have that Python doesn't, and vice versa?What does map(&:name) mean in Ruby?How to write to file in Ruby?Ruby: What is the easiest way to remove the first element from an array?What is attr_accessor in Ruby?Ruby shortest way to write rnd hex

What happens to unproductive professors?

Postgres Trigram acting strange for specific characters

What is a "Lear Processor" and how did it work?

WTB Horizon 47c - small crack in the middle of the tire

How many tone holes are there actually in different orchestral woodwind instruments?

Why is a mixture of two normally distributed variables only bimodal if their means differ by at least two times the common standard deviation?

What adjective means "accurately representitive of reality"?

Are there languages where this "is" phrase is reversed?

Reverse dots and boxes

Misspelling my name on my mathematical publications

What could cause the sea level to massively decrease?

Redundancy in rappel systems

Found and corrected a mistake on someone's else paper -- praxis?

What is the minimum time required for final wash in film development?

Is this a reference to the film Alien in the novel 2010 Odyssey Two?

What the real concept of Static keyword in perspective of Embedded C. See below code

Does a wizard need their hands free in order to cause their familiar from the Find Familiar spell to reappear?

Is there a method for differentiating informative comments from commented out code?

Why weren't bootable game disks ever a thing on the IBM PC?

Data Encryption by Application vs Data Encryption in Database

Efficiently defining a SparseArray function

What is this little owl-like bird?

Why does wrapping Aluminium foil around my food help it keep warm, aluminium be good conductor should have no effect?

Do I have to worry about delays in international trains? (UK, Belgium, Germany)



What is the shortest way in ruby to write these four instructions?


Rails: update_attribute vs update_attributesWhat is the easiest way to duplicate an activerecord record?What is the “right” way to iterate through an array in Ruby?What is the difference between include and require in Ruby?How to write a switch statement in RubyWhat does Ruby have that Python doesn't, and vice versa?What does map(&:name) mean in Ruby?How to write to file in Ruby?Ruby: What is the easiest way to remove the first element from an array?What is attr_accessor in Ruby?Ruby shortest way to write rnd hex






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















u = User.email_equals("tabaluga@gmail.com").first
s = u.setting
s.regular_info = false
s.save


Does anyone know how to write it shorter? Perhaps in one line? That would be awesome. Thanks, merci :)










share|improve this question
























  • If you're using Rails then you should tag your question as such, otherwise there's no way of knowing how your code can be changed

    – Gareth
    Oct 15 '11 at 13:01

















2















u = User.email_equals("tabaluga@gmail.com").first
s = u.setting
s.regular_info = false
s.save


Does anyone know how to write it shorter? Perhaps in one line? That would be awesome. Thanks, merci :)










share|improve this question
























  • If you're using Rails then you should tag your question as such, otherwise there's no way of knowing how your code can be changed

    – Gareth
    Oct 15 '11 at 13:01













2












2








2


0






u = User.email_equals("tabaluga@gmail.com").first
s = u.setting
s.regular_info = false
s.save


Does anyone know how to write it shorter? Perhaps in one line? That would be awesome. Thanks, merci :)










share|improve this question
















u = User.email_equals("tabaluga@gmail.com").first
s = u.setting
s.regular_info = false
s.save


Does anyone know how to write it shorter? Perhaps in one line? That would be awesome. Thanks, merci :)







ruby-on-rails ruby






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 15 '11 at 13:05









Kathy Van Stone

20.3k2 gold badges28 silver badges39 bronze badges




20.3k2 gold badges28 silver badges39 bronze badges










asked Oct 15 '11 at 12:56









tabalugatabaluga

6722 gold badges16 silver badges26 bronze badges




6722 gold badges16 silver badges26 bronze badges












  • If you're using Rails then you should tag your question as such, otherwise there's no way of knowing how your code can be changed

    – Gareth
    Oct 15 '11 at 13:01

















  • If you're using Rails then you should tag your question as such, otherwise there's no way of knowing how your code can be changed

    – Gareth
    Oct 15 '11 at 13:01
















If you're using Rails then you should tag your question as such, otherwise there's no way of knowing how your code can be changed

– Gareth
Oct 15 '11 at 13:01





If you're using Rails then you should tag your question as such, otherwise there's no way of knowing how your code can be changed

– Gareth
Oct 15 '11 at 13:01












3 Answers
3






active

oldest

votes


















4














User.email_equals("tabaluga@gmail.com").first.setting.update_attribute(:regular_info, false)


(don't have a console handy to check, but think that should work ..)






share|improve this answer























  • Great! Thankyou! :)

    – tabaluga
    Oct 15 '11 at 13:10


















4














I'm not sure how do you define your email_equals method, but Rails provides Dynamic attribute-based finders which returns the first match or nil for not found.



User.find_by_email("tabaluga@gmail.com").setting.update_attribute(:regular_info, false)





share|improve this answer























  • This is the best answer (IMHO).

    – Mischa
    Oct 15 '11 at 15:18











  • Use try to handle the nil results returned by find_by_email and settings methods, i.e. find_by_email("tabaluga@gmail.com").try(:setting).try(:update_attribute, :regular_info, false)

    – Harish Shetty
    Oct 15 '11 at 18:59











  • @KandadaBoggu, if I want to handle nil in this case, then I prefer not using try. I prefer using find_by_email!, so I could rescue the exception or I split it into two lines with condition instead.

    – Samnang
    Oct 15 '11 at 19:34











  • The find_by_email call doesn't throw exception. It will simply return nil upon not finding a match. Only find method throws RecordNotFound exception.

    – Harish Shetty
    Oct 16 '11 at 5:33











  • @KandadaBoggu, find_by_email doesn't raise the exception when it can't found, but find_by_email! does raise the exception.

    – Samnang
    Nov 1 '11 at 3:04



















0














Although you can write this in one line, I would recommend against it. This makes your code less readable and maintainable.



This is also an isolated example; realistically the email would not be hardcoded, it would be stored in its own variable and might be an argument.



One Liner



User.email_equals("tabaluga@gmail.com").first.setting.update_attribute(:regular_info, false)



Two Liner



user = User.email_equals("tabaluga@gmail.com").first
user.setting.update_attribute(:regular_info, false)

# Or...
user_setting = User.email_equals("tabaluga@gmail.com").first.setting
user_setting.update_attribute(:regular_info, false)

# More readable, but not maintainable
User.email_equals("tabaluga@gmail.com").first
.setting.update_attribute(:regular_info, false)


Three Liner



user = User.email_equals("tabaluga@gmail.com").first
setting = user.setting
setting.update_attribute(:regular_info, false)





share|improve this answer



























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f7777941%2fwhat-is-the-shortest-way-in-ruby-to-write-these-four-instructions%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4














    User.email_equals("tabaluga@gmail.com").first.setting.update_attribute(:regular_info, false)


    (don't have a console handy to check, but think that should work ..)






    share|improve this answer























    • Great! Thankyou! :)

      – tabaluga
      Oct 15 '11 at 13:10















    4














    User.email_equals("tabaluga@gmail.com").first.setting.update_attribute(:regular_info, false)


    (don't have a console handy to check, but think that should work ..)






    share|improve this answer























    • Great! Thankyou! :)

      – tabaluga
      Oct 15 '11 at 13:10













    4












    4








    4







    User.email_equals("tabaluga@gmail.com").first.setting.update_attribute(:regular_info, false)


    (don't have a console handy to check, but think that should work ..)






    share|improve this answer













    User.email_equals("tabaluga@gmail.com").first.setting.update_attribute(:regular_info, false)


    (don't have a console handy to check, but think that should work ..)







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Oct 15 '11 at 13:00









    chrispandachrispanda

    2,9641 gold badge15 silver badges22 bronze badges




    2,9641 gold badge15 silver badges22 bronze badges












    • Great! Thankyou! :)

      – tabaluga
      Oct 15 '11 at 13:10

















    • Great! Thankyou! :)

      – tabaluga
      Oct 15 '11 at 13:10
















    Great! Thankyou! :)

    – tabaluga
    Oct 15 '11 at 13:10





    Great! Thankyou! :)

    – tabaluga
    Oct 15 '11 at 13:10













    4














    I'm not sure how do you define your email_equals method, but Rails provides Dynamic attribute-based finders which returns the first match or nil for not found.



    User.find_by_email("tabaluga@gmail.com").setting.update_attribute(:regular_info, false)





    share|improve this answer























    • This is the best answer (IMHO).

      – Mischa
      Oct 15 '11 at 15:18











    • Use try to handle the nil results returned by find_by_email and settings methods, i.e. find_by_email("tabaluga@gmail.com").try(:setting).try(:update_attribute, :regular_info, false)

      – Harish Shetty
      Oct 15 '11 at 18:59











    • @KandadaBoggu, if I want to handle nil in this case, then I prefer not using try. I prefer using find_by_email!, so I could rescue the exception or I split it into two lines with condition instead.

      – Samnang
      Oct 15 '11 at 19:34











    • The find_by_email call doesn't throw exception. It will simply return nil upon not finding a match. Only find method throws RecordNotFound exception.

      – Harish Shetty
      Oct 16 '11 at 5:33











    • @KandadaBoggu, find_by_email doesn't raise the exception when it can't found, but find_by_email! does raise the exception.

      – Samnang
      Nov 1 '11 at 3:04
















    4














    I'm not sure how do you define your email_equals method, but Rails provides Dynamic attribute-based finders which returns the first match or nil for not found.



    User.find_by_email("tabaluga@gmail.com").setting.update_attribute(:regular_info, false)





    share|improve this answer























    • This is the best answer (IMHO).

      – Mischa
      Oct 15 '11 at 15:18











    • Use try to handle the nil results returned by find_by_email and settings methods, i.e. find_by_email("tabaluga@gmail.com").try(:setting).try(:update_attribute, :regular_info, false)

      – Harish Shetty
      Oct 15 '11 at 18:59











    • @KandadaBoggu, if I want to handle nil in this case, then I prefer not using try. I prefer using find_by_email!, so I could rescue the exception or I split it into two lines with condition instead.

      – Samnang
      Oct 15 '11 at 19:34











    • The find_by_email call doesn't throw exception. It will simply return nil upon not finding a match. Only find method throws RecordNotFound exception.

      – Harish Shetty
      Oct 16 '11 at 5:33











    • @KandadaBoggu, find_by_email doesn't raise the exception when it can't found, but find_by_email! does raise the exception.

      – Samnang
      Nov 1 '11 at 3:04














    4












    4








    4







    I'm not sure how do you define your email_equals method, but Rails provides Dynamic attribute-based finders which returns the first match or nil for not found.



    User.find_by_email("tabaluga@gmail.com").setting.update_attribute(:regular_info, false)





    share|improve this answer













    I'm not sure how do you define your email_equals method, but Rails provides Dynamic attribute-based finders which returns the first match or nil for not found.



    User.find_by_email("tabaluga@gmail.com").setting.update_attribute(:regular_info, false)






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Oct 15 '11 at 15:06









    SamnangSamnang

    2,7584 gold badges30 silver badges45 bronze badges




    2,7584 gold badges30 silver badges45 bronze badges












    • This is the best answer (IMHO).

      – Mischa
      Oct 15 '11 at 15:18











    • Use try to handle the nil results returned by find_by_email and settings methods, i.e. find_by_email("tabaluga@gmail.com").try(:setting).try(:update_attribute, :regular_info, false)

      – Harish Shetty
      Oct 15 '11 at 18:59











    • @KandadaBoggu, if I want to handle nil in this case, then I prefer not using try. I prefer using find_by_email!, so I could rescue the exception or I split it into two lines with condition instead.

      – Samnang
      Oct 15 '11 at 19:34











    • The find_by_email call doesn't throw exception. It will simply return nil upon not finding a match. Only find method throws RecordNotFound exception.

      – Harish Shetty
      Oct 16 '11 at 5:33











    • @KandadaBoggu, find_by_email doesn't raise the exception when it can't found, but find_by_email! does raise the exception.

      – Samnang
      Nov 1 '11 at 3:04


















    • This is the best answer (IMHO).

      – Mischa
      Oct 15 '11 at 15:18











    • Use try to handle the nil results returned by find_by_email and settings methods, i.e. find_by_email("tabaluga@gmail.com").try(:setting).try(:update_attribute, :regular_info, false)

      – Harish Shetty
      Oct 15 '11 at 18:59











    • @KandadaBoggu, if I want to handle nil in this case, then I prefer not using try. I prefer using find_by_email!, so I could rescue the exception or I split it into two lines with condition instead.

      – Samnang
      Oct 15 '11 at 19:34











    • The find_by_email call doesn't throw exception. It will simply return nil upon not finding a match. Only find method throws RecordNotFound exception.

      – Harish Shetty
      Oct 16 '11 at 5:33











    • @KandadaBoggu, find_by_email doesn't raise the exception when it can't found, but find_by_email! does raise the exception.

      – Samnang
      Nov 1 '11 at 3:04

















    This is the best answer (IMHO).

    – Mischa
    Oct 15 '11 at 15:18





    This is the best answer (IMHO).

    – Mischa
    Oct 15 '11 at 15:18













    Use try to handle the nil results returned by find_by_email and settings methods, i.e. find_by_email("tabaluga@gmail.com").try(:setting).try(:update_attribute, :regular_info, false)

    – Harish Shetty
    Oct 15 '11 at 18:59





    Use try to handle the nil results returned by find_by_email and settings methods, i.e. find_by_email("tabaluga@gmail.com").try(:setting).try(:update_attribute, :regular_info, false)

    – Harish Shetty
    Oct 15 '11 at 18:59













    @KandadaBoggu, if I want to handle nil in this case, then I prefer not using try. I prefer using find_by_email!, so I could rescue the exception or I split it into two lines with condition instead.

    – Samnang
    Oct 15 '11 at 19:34





    @KandadaBoggu, if I want to handle nil in this case, then I prefer not using try. I prefer using find_by_email!, so I could rescue the exception or I split it into two lines with condition instead.

    – Samnang
    Oct 15 '11 at 19:34













    The find_by_email call doesn't throw exception. It will simply return nil upon not finding a match. Only find method throws RecordNotFound exception.

    – Harish Shetty
    Oct 16 '11 at 5:33





    The find_by_email call doesn't throw exception. It will simply return nil upon not finding a match. Only find method throws RecordNotFound exception.

    – Harish Shetty
    Oct 16 '11 at 5:33













    @KandadaBoggu, find_by_email doesn't raise the exception when it can't found, but find_by_email! does raise the exception.

    – Samnang
    Nov 1 '11 at 3:04






    @KandadaBoggu, find_by_email doesn't raise the exception when it can't found, but find_by_email! does raise the exception.

    – Samnang
    Nov 1 '11 at 3:04












    0














    Although you can write this in one line, I would recommend against it. This makes your code less readable and maintainable.



    This is also an isolated example; realistically the email would not be hardcoded, it would be stored in its own variable and might be an argument.



    One Liner



    User.email_equals("tabaluga@gmail.com").first.setting.update_attribute(:regular_info, false)



    Two Liner



    user = User.email_equals("tabaluga@gmail.com").first
    user.setting.update_attribute(:regular_info, false)

    # Or...
    user_setting = User.email_equals("tabaluga@gmail.com").first.setting
    user_setting.update_attribute(:regular_info, false)

    # More readable, but not maintainable
    User.email_equals("tabaluga@gmail.com").first
    .setting.update_attribute(:regular_info, false)


    Three Liner



    user = User.email_equals("tabaluga@gmail.com").first
    setting = user.setting
    setting.update_attribute(:regular_info, false)





    share|improve this answer





























      0














      Although you can write this in one line, I would recommend against it. This makes your code less readable and maintainable.



      This is also an isolated example; realistically the email would not be hardcoded, it would be stored in its own variable and might be an argument.



      One Liner



      User.email_equals("tabaluga@gmail.com").first.setting.update_attribute(:regular_info, false)



      Two Liner



      user = User.email_equals("tabaluga@gmail.com").first
      user.setting.update_attribute(:regular_info, false)

      # Or...
      user_setting = User.email_equals("tabaluga@gmail.com").first.setting
      user_setting.update_attribute(:regular_info, false)

      # More readable, but not maintainable
      User.email_equals("tabaluga@gmail.com").first
      .setting.update_attribute(:regular_info, false)


      Three Liner



      user = User.email_equals("tabaluga@gmail.com").first
      setting = user.setting
      setting.update_attribute(:regular_info, false)





      share|improve this answer



























        0












        0








        0







        Although you can write this in one line, I would recommend against it. This makes your code less readable and maintainable.



        This is also an isolated example; realistically the email would not be hardcoded, it would be stored in its own variable and might be an argument.



        One Liner



        User.email_equals("tabaluga@gmail.com").first.setting.update_attribute(:regular_info, false)



        Two Liner



        user = User.email_equals("tabaluga@gmail.com").first
        user.setting.update_attribute(:regular_info, false)

        # Or...
        user_setting = User.email_equals("tabaluga@gmail.com").first.setting
        user_setting.update_attribute(:regular_info, false)

        # More readable, but not maintainable
        User.email_equals("tabaluga@gmail.com").first
        .setting.update_attribute(:regular_info, false)


        Three Liner



        user = User.email_equals("tabaluga@gmail.com").first
        setting = user.setting
        setting.update_attribute(:regular_info, false)





        share|improve this answer















        Although you can write this in one line, I would recommend against it. This makes your code less readable and maintainable.



        This is also an isolated example; realistically the email would not be hardcoded, it would be stored in its own variable and might be an argument.



        One Liner



        User.email_equals("tabaluga@gmail.com").first.setting.update_attribute(:regular_info, false)



        Two Liner



        user = User.email_equals("tabaluga@gmail.com").first
        user.setting.update_attribute(:regular_info, false)

        # Or...
        user_setting = User.email_equals("tabaluga@gmail.com").first.setting
        user_setting.update_attribute(:regular_info, false)

        # More readable, but not maintainable
        User.email_equals("tabaluga@gmail.com").first
        .setting.update_attribute(:regular_info, false)


        Three Liner



        user = User.email_equals("tabaluga@gmail.com").first
        setting = user.setting
        setting.update_attribute(:regular_info, false)






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 26 at 0:16

























        answered Mar 25 at 23:57









        CTS_AECTS_AE

        3,0472 gold badges29 silver badges40 bronze badges




        3,0472 gold badges29 silver badges40 bronze badges



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f7777941%2fwhat-is-the-shortest-way-in-ruby-to-write-these-four-instructions%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript