views:

552

answers:

1

For the life of me, I can't figure out how to do this. I have a model for which the stored value is a path to a resource on the file system. I'd like the model to write and fetch the resource from the file system, so I naturally want to override the getter and setters to do this. How do I then get at the underlying value that's in the db?

class MyModel < ActiveRecord::Base
  require 'fileutils'
  def myThing=(val)
    handle = File.open(_____, 'w')
    handle.write(val)
  end
end

What goes where the underscores are? I've seen 'write_attribute' used to do this, but it looks to be deprecated. Any ideas? Ditto for the getter, of course.

Also, if I'm barking up the wrong tree regarding overriding, I'd love to hear the better technique. The only thing that's not an option is to store the value directly in the db.

Thanks!

A: 

This should do it: handle = File.open(attributes["myThing"])

I'd think about renaming the column to "my_thing_path" (or myThingPath, if you must), though.

OP, here. While this answer didn't work for my version of Rails, it did put me on the right track. self["myThing"] seems to have worked!
Mike