I have a module that I'm including in several models with this content:
self.class.find_by_foo(bar)
Everything was fine until I started using STI. That line should always generate the query
select * from table where foo=bar"
and not
select * from table where foo=bar AND type="Whatever"
Is there a simple way to avoid it?
I though of two solutions. Walking up the class hierarchy until I find the top-most class before ActiveRecord::Base
or run the query by hand, like:
self.class.find_by_sql("select * from #{self.class.table_name} where foo=bar")
I don't like either solution. Is there a better one?
self.class
unexpectedly returned the objects parent class, in Rails 5self.class
returns, as excepted, the models class.self.class.base_class
can be used to get the parent class. – Manganin