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
Multi tool use
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;
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
add a comment |
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
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
add a comment |
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
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
ruby-on-rails ruby
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
add a comment |
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
add a comment |
3 Answers
3
active
oldest
votes
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 ..)
Great! Thankyou! :)
– tabaluga
Oct 15 '11 at 13:10
add a comment |
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)
This is the best answer (IMHO).
– Mischa
Oct 15 '11 at 15:18
Usetry
to handle thenil
results returned byfind_by_email
andsettings
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
Thefind_by_email
call doesn't throw exception. It will simply returnnil
upon not finding a match. Onlyfind
method throwsRecordNotFound
exception.
– Harish Shetty
Oct 16 '11 at 5:33
@KandadaBoggu,find_by_email
doesn't raise the exception when it can't found, butfind_by_email!
does raise the exception.
– Samnang
Nov 1 '11 at 3:04
|
show 1 more comment
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)
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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 ..)
Great! Thankyou! :)
– tabaluga
Oct 15 '11 at 13:10
add a comment |
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 ..)
Great! Thankyou! :)
– tabaluga
Oct 15 '11 at 13:10
add a comment |
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 ..)
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 ..)
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
add a comment |
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
add a comment |
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)
This is the best answer (IMHO).
– Mischa
Oct 15 '11 at 15:18
Usetry
to handle thenil
results returned byfind_by_email
andsettings
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
Thefind_by_email
call doesn't throw exception. It will simply returnnil
upon not finding a match. Onlyfind
method throwsRecordNotFound
exception.
– Harish Shetty
Oct 16 '11 at 5:33
@KandadaBoggu,find_by_email
doesn't raise the exception when it can't found, butfind_by_email!
does raise the exception.
– Samnang
Nov 1 '11 at 3:04
|
show 1 more comment
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)
This is the best answer (IMHO).
– Mischa
Oct 15 '11 at 15:18
Usetry
to handle thenil
results returned byfind_by_email
andsettings
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
Thefind_by_email
call doesn't throw exception. It will simply returnnil
upon not finding a match. Onlyfind
method throwsRecordNotFound
exception.
– Harish Shetty
Oct 16 '11 at 5:33
@KandadaBoggu,find_by_email
doesn't raise the exception when it can't found, butfind_by_email!
does raise the exception.
– Samnang
Nov 1 '11 at 3:04
|
show 1 more comment
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)
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)
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
Usetry
to handle thenil
results returned byfind_by_email
andsettings
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
Thefind_by_email
call doesn't throw exception. It will simply returnnil
upon not finding a match. Onlyfind
method throwsRecordNotFound
exception.
– Harish Shetty
Oct 16 '11 at 5:33
@KandadaBoggu,find_by_email
doesn't raise the exception when it can't found, butfind_by_email!
does raise the exception.
– Samnang
Nov 1 '11 at 3:04
|
show 1 more comment
This is the best answer (IMHO).
– Mischa
Oct 15 '11 at 15:18
Usetry
to handle thenil
results returned byfind_by_email
andsettings
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
Thefind_by_email
call doesn't throw exception. It will simply returnnil
upon not finding a match. Onlyfind
method throwsRecordNotFound
exception.
– Harish Shetty
Oct 16 '11 at 5:33
@KandadaBoggu,find_by_email
doesn't raise the exception when it can't found, butfind_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
|
show 1 more comment
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)
add a comment |
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)
add a comment |
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)
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)
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
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
mqxZOzY1,jYc q1nRU8Qh vK,4nvo,9IpGBzSt,MLzP,wB72
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