ruby-on-rails-3.2rails-activerecordproxy-classes

Rails: Make a remote proxy persist with active record


A course proxy knows the id of a course on a remote system and fetches that info when it is accessed. Example code:

class Course < BasicObject
  attr_accessor :course_id, :course_info
[..]
def method_missing(*a, &b)
  if course_info.nil?
    load_course_info
  end
  course_info.send(*a, &b)
end
[...]

def load_course_info
 # this will fetch the course information from a remote server
 # using the course_id
end

My problem: I would like to use active record to make the proxy's course_id persist (not the course info) so that the proxy remembers how to get the info again at a later stage.

Course inherits from BasicObject so I can't do

Course < ActiveRecord::Base

I guess ActiveRecord::Base has far too many methods to be useful as a proxy.

What is the solution?


Solution

  • Found the answer at: https://solnic.codes/2011/08/01/making-activerecord-models-thin/

    Piotr says (if I understand correctly) that making models persist by directly inheriting from active record leads to polluting the responsibilities of the model/domain objects.

    He shows a way how to solve this problem, which is a more general solution than what my question was concerned with.