Skip to content

Return ActiveRecord::Relation from a tableless model #15

@FredPerrin

Description

@FredPerrin

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions