Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I write this more "ruby-ish"?

I got the following snippet

Class Artist < ActiveRecord
   attr_accessible :solo #boolean

  def change_solo!
    if self.solo == false
     self.solo = true
    else
     self.solo = false
    end
   self.save
  end

end

Is there a better way to write that? Thanks in advance cheers tabaluga

like image 473
tabaluga Avatar asked Aug 15 '26 08:08

tabaluga


1 Answers

Don't write this method. Use ActiveRecord::Base#toggle! instead:

artist_object.toggle!(:solo)

This method also exists in Rails 2, if you haven't upgraded yet.

like image 67
wuputah Avatar answered Aug 17 '26 00:08

wuputah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!