Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return ActiveRecord::Relation from a tableless model #15

Open
FredPerrin opened this issue Apr 8, 2014 · 0 comments
Open

Return ActiveRecord::Relation from a tableless model #15

FredPerrin opened this issue Apr 8, 2014 · 0 comments

Comments

@FredPerrin
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant