Change FROM in all queries for ActiveRecord model

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
  # methods common to both types
end

class FooTypeA < Foo
  default_scope -> { where is_type_a: true }
  # methods for type A
end

class FooTypeB < Foo
  default_scope -> { where is_type_a: false }
  # methods for type B
end

, , . , SQL-. , , .

, self.table_name, , , FROM SQL- , FooTypeA : SELECT foo_as.* FROM foos AS foo_as ...

, , .

+4
1

ActiveRecord.from ?

( mysql) , , , .

0

All Articles