1
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.

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
tabaluga
  • 1,377
  • 4
  • 20
  • 27
  • 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 Answers3

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 ..)

chrispanda
  • 3,204
  • 1
  • 21
  • 23
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)
Samnang
  • 5,536
  • 6
  • 35
  • 47
  • 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 05: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 01 '11 at 03:04
  • @Samnang `find_by_email` and `find_by_email!` are separate methods. So it is expected that they behave differently. Any method that ends with a `!` in ruby is supposed to modify the data or throw an exception. – Harish Shetty Nov 01 '11 at 14:51
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)
CTS_AE
  • 12,987
  • 8
  • 62
  • 63