I am working on a rails project that is connected to a third-party MySQL database that I cannot change the schema for . Until now, I could train everything in rails and play them well, but I ran into an interesting problem.
I have a table, we will call it foos. I have an ActiveRecord model called Foothat uses this table. The problem is that this table represents two similar, but different types of records. We will call them types Foo A and Foo of type B. To get around this, I created two classes FooTypeAand FooTypeBthat inherit from Fooand have default scopes, so they only contain entries of their respective types.
My code looks something like this:
class Foo < ActiveRecord::Base
end
class FooTypeA < Foo
default_scope -> { where is_type_a: true }
end
class FooTypeB < Foo
default_scope -> { where is_type_a: false }
end
, , . , SQL-. , , .
, self.table_name, , , FROM SQL- , FooTypeA : SELECT foo_as.* FROM foos AS foo_as ...
, , .