Search code examples
ruby-on-railsrubyruby-on-rails-3model-view-controllerruby-on-rails-plugins

Rails: Model attributes from XML


I am new to Rails and would like your suggestions for the following.

I have a model say Video. Certain attributes of the Video are stored in MySQL database while some of other attributes are stored in an associated XML file on the server.

For example, id, title and slug is stored in the database while duration, thumbnails, description, etc are stored in an XML file.

How should I implement my model so that I can access all the attributes of the video and not only those stored in the database?


Solution

  • Use after_find or after_initialize to read the attributes from the file and populate some virtual attributes in your object. Then use the after_save to check if the attributes have changed and if so write the new ones back out to the file. More detail in the ActiveRecord::Callbacks docs, here.