Rails:使远程代理持久保留活动记录

问题描述 投票:0回答:1

课程代理知道远程系统上课程的ID,在访问时获取该信息。示例代码:

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

我的问题:我想使用活动记录来创建代理的course_id坚持(不是课程信息),以便代理记住在以后的阶段如何再次获取信息。

课程从BasicObject继承,所以我不能这样做

Course < ActiveRecord::Base

我想ActiveRecord :: Base有太多方法无法用作代理。

解决方案是什么?

ruby-on-rails-3.2 rails-activerecord proxy-classes
1个回答
0
投票

在以下位置找到了答案:https://solnic.codes/2011/08/01/making-activerecord-models-thin/

Piotr说(如果我理解正确的话),通过直接从活动记录中继承来使模型持久化会污染模型/域对象的职责。

他展示了一种解决此问题的方法,这比我所关注的问题更通用。

© www.soinside.com 2019 - 2024. All rights reserved.