ActiveRecord Extension With Different Tables

11 views
Skip to first unread message

Usman Shahid

unread,
Oct 14, 2016, 9:58:00 AM10/14/16
to rubyonra...@googlegroups.com
Hi,

Is it possible in rails to write a generic ActiveRecord::Base child class which is bound with a table of our choice on runtime? for example if it has two functions 'foo' and 'bar' then i should be able to provide a table name on runtime e.g. 'table-a' or 'table-b' and execute these functions on it?

Regards,
Usman

Greg Navis

unread,
Oct 14, 2016, 7:57:26 PM10/14/16
to rubyonra...@googlegroups.com
Probably doable (hackable) but I think it’d be extremely unidiomatic Rails. If you could tell us what you’re trying to achieve we might be able to suggest a better solution.

Cheers
Greg

Usman Shahid

unread,
Oct 15, 2016, 5:08:00 AM10/15/16
to rubyonra...@googlegroups.com
I cannot find an alternative to this. What i'm trying to do is write a generic interface which can answer generic queries like search based on specific column and aggregate queries etc. Underlying table is provided to the Model on initialization and I want it hooked with that table so that I can run these active record generic queries (and some generic extensions) on the underlying table.

Also, how do I hack it? Is there any downside of doing it?

Regards

Greg

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/DDCF52E7-47BE-4B82-9823-885C7C9A8D7A%40gregnavis.com.
For more options, visit https://groups.google.com/d/optout.

Colin Law

unread,
Oct 15, 2016, 6:32:24 AM10/15/16
to Ruby on Rails: Talk
On 15 October 2016 at 10:07, Usman Shahid <usman....@gmail.com> wrote:
I cannot find an alternative to this. What i'm trying to do is write a generic interface which can answer generic queries like search based on specific column and aggregate queries etc. Underlying table is provided to the Model on initialization and I want it hooked with that table so that I can run these active record generic queries (and some generic extensions) on the underlying table.

Also, how do I hack it? Is there any downside of doing it?

If you want methods available in a number of models you can derive an intermediary class from ActiveRecord base, provide the methods in there, and derive your models from the intermediary.

Colin

Greg Navis

unread,
Oct 15, 2016, 8:24:22 AM10/15/16
to rubyonra...@googlegroups.com
What Colin said. Alternatively, you could write a concern and include it in models you want to support in the search feature.

Usman Shahid

unread,
Oct 15, 2016, 9:10:48 AM10/15/16
to rubyonra...@googlegroups.com
Actually these tables and models are dynamically added to the database, every addition requires creating a new model in the system which contains nothing but the table name. I'm trying to avoid that by creating a single model which can handle multiple tables given a table name using ActiveRecord query interface.

On 15 October 2016 at 17:23, Greg Navis <con...@gregnavis.com> wrote:
What Colin said. Alternatively, you could write a concern and include it in models you want to support in the search feature.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.

nanaya

unread,
Oct 15, 2016, 9:43:35 AM10/15/16
to rubyonra...@googlegroups.com
Hi,

On Sat, Oct 15, 2016, at 22:10, Usman Shahid wrote:
> Actually these tables and models are dynamically added to the database,
> every addition requires creating a new model in the system which contains
> nothing but the table name. I'm trying to avoid that by creating a single
> model which can handle multiple tables given a table name using
> ActiveRecord query interface.
>


You're probably better off using arel[1] directly instead of bending ar
to your need.

[1] https://github.com/rails/arel
Reply all
Reply to author
Forward
0 new messages