开发者

Best way to have a model with two different data sources

开发者 https://www.devze.com 2023-04-04 00:06 出处:网络
I have a rails model where some part of data comes from database and other by calling a restful web service. Is there a way to use both ActiveRecord and ActiveResource in the same model to achieve thi

I have a rails model where some part of data comes from database and other by calling a restful web service. Is there a way to use both ActiveRecord and ActiveResource in the same model to achieve this?

Ideally I would like to load the attributes that it gets from web service lazily (when accessed in controller) and only load the db attributes by default. Is it best if I create two models one th开发者_如何转开发at accesses service using ActiveResource and one with ActiveResource which :has_one of the active resource model. Also, how to make sure I only call the service when only activeresource methods are accessed (like a before_filter but for only those methods - if I have a relationship with other model, doesn't rails already do that)?

Thanks in advance.


Have a look at the delegate method... This way you can proxy methods to another model.

http://blog.wyeworks.com/2009/6/4/rails-delegate-method

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号