Create a method like this:
class Bar def nullify! update_attribute :foo_id, nil end end
And now you can call it on any instance of the bar. To make it fit your example, you can do this:
def remove !self.foo_id.nil? end def remove= bool update_attribute :foo_id, nil if bool end
This version will allow you to pass a parameter equal to true or false, so you can implement it as a flag in forms. Hope this helps!
UPDATE: I added a blog post that details how non-attributes can be used as form elements in rails, adding accessors to your model:
Dynamic form elements in Ruby on Rails
It includes a working Rails 3 application that shows how all the parts work together.
source share