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

ActiveRecord Inheritance without STI? #5541

Closed
joegoggins opened this issue Mar 21, 2012 · 5 comments
Closed

ActiveRecord Inheritance without STI? #5541

joegoggins opened this issue Mar 21, 2012 · 5 comments

Comments

@joegoggins
Copy link
Contributor

If you have:

class SuperClass < ActiveRecord::Base
end
class Child < SuperClass
  self.table_name = 'the_table_i_really_want'
end

and you do:

Child.create

you get:

ActiveRecord::StatementInvalid: Could not find table 'super_classes'

which means the:

self.table_name='the_table_i_really_want'

statement isn't honored.

I'm not sure if this is intentional behavior or not. If it is intentional it would be nice if self.table_name= raised an exception instead of failing later, once you start to use the child classes.

Alternatively, it would be even better if you could optionally bypass STI with something like:

class SuperClass < ActiveRecord::Base
  self.bypass_single_table_inheritance = true
end

I have several situations where I've really needed inheritance in ActiveRecord without STI (where mixins don't work very well), and the bit of googling around I've done seems to confirm there are others out there who would appreciate it too. If there is appetite on the Rails core team to make this happen, I would love to help out.

@runlevel5
Copy link
Contributor

I find having multiple tables for an STI is not the right way. I think having the bypass option would overcomplicate the architect in which mixins should be used.

@joegoggins
Copy link
Contributor Author

I agree with you on multi table inheritance, STI is superior. However, I'm just interested in sharing functionality like the same connection to a different db across a collection of ActiveRecord models, how would u do that efficiently w mixins?

On Mar 21, 2012, at 9:13 PM, Trung Lêreply@reply.github.com wrote:

I find having multiple tables for an STI is not the right way. I think having the bypass option would overcomplex the architect in which mixins should be used.


Reply to this email directly or view it on GitHub:
#5541 (comment)

@runlevel5
Copy link
Contributor

@joegoggins I haven't bumped into such cases, so please excuse me if my solution turns out quite clumsy. I'd create a YAML file with structure like this:

table_1:
  model: ar_class_name
  connection: production

table_2
  model: ar_class_name2
  connection: name_of_connection_in_database_yml

then I write a module to lookup for the right connection given that class name constant is provided.

@markmcspadden
Copy link
Contributor

I think that the following will do what you are looking for:

class SuperClass < ActiveRecord::Base
  self.abstract_class = true
end

@joegoggins
Copy link
Contributor Author

@markmcspadden thanks for the feedback, this fixes my problem. I've also added a commit to docrails that clarifies when to use this functionality:
https://github.com/lifo/docrails/commit/65a3020851f235cac88afa215b805c2ed43f2639

@joneslee85 thanks for the suggestion--I think @markmcspadden 's recommendation will work for this use case, but I'll comment on this issue, if I end up implementing some variant of your implementation idea.

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

3 participants