softace / activerecord-tableless

Bringing together the different tableless model implementations into a single gem/plugin
Other
113 stars 41 forks source link

Return ActiveRecord::Relation from a tableless model #15

Open FredPerrin opened 10 years ago

FredPerrin commented 10 years ago

Hi,

In my application (Rails 4), the User model is not stored in the 'local' database but is on a backoffice application.

So, I use a tableless model and I have a module 'ApiUser' to access my backoffice.

The User model (tableless) use this module for every operation (read or write data...) on the backoffice.

Of course, I need every ActiveRecord methods, like new, save, update, destroy, all, find....

I'm not sure it's the good way, but in my model I implement a 'all' method (and a corresponding function in the API to retrieve data from the backoffice). For the moment, in the model, I create an array populated with data by doing something like :

result = call_to_api_to_retrieve_datas
users = []
result[:user].each { |user| users << User.new(user) }
return users

It seems to work, but I have relation (has_many) to other models and some automatic functions don't work (each, index_by, map...) on the relation.

My question : is reimplementig each ActiveRecord method is the good way to do and how to simulate an ActiveRecord::Relation like a real ActiveRecord model ?

If you need more informations on my model, let me know.

Thanks