3

I have two rails application base1 and base2.

base1 uses db1 and have multiple tenants inside this database, its using apartment gem.

base2 is single tenant application and has database db2 as primary, and also uses data from db1.

Now the problem is, base2 application established connection to db1 and i can get the data db1 data in public tenant in base2 application.
How to get data from different schemas of db1 in base2 application?

8
  • Do you frequently need to change schema in your base2 app's models? Also, do db1 and db2 share any table names? I think i can help you here but i need more details. Also is there any writes being done on db1 from base 2 app? Commented Jun 21, 2019 at 7:37
  • base 2 is a single tenant application which is used to get the data from base1 application which is a multitenant application. i dnt want to change schema of base2 application it have its own schema. Commented Jun 21, 2019 at 7:59
  • No, i think you didn't understand my comment, let me rephrase. When you will access db1 from base2, you want to tell it to look specific table in specific schema(not in public). So my question is, when accessing db1 from base2, do you need to access same table data from different schemas or just want data from one schema? My second question was, if you need to do any writes on db1 from base2 app. Commented Jun 21, 2019 at 8:02
  • sorry.now i got your comment. i only need data from one schema at a time. and i also want to write on db1 from base2 app Commented Jun 21, 2019 at 8:06
  • Ok, there is a table_name option which you can set to something specific in model like self.table_name = 'schema_name.table_name' but this will force use of same schema for all queries. Does this work for you or you want to adjust schema when querying everytime? Commented Jun 21, 2019 at 8:11

1 Answer 1

6

As per discussions in comments.

def self.with_schema(schema_name)
    class_name = self.name + schema_name.camelize
    table_name = self.table_name
    if !Object.const_defined?(class_name)
      Object.const_set(
        class_name, Class.new(self) do
          self.table_name = "#{schema_name}.#{table_name}"
        end
      )
    end
    class_name.constantize
  end

Add this to your application record and you can do things like: Data.schema('schema_name').all

Sign up to request clarification or add additional context in comments.

4 Comments

Yeah !!!! this worked,.... Thank you so much!!!!!! `Data.with_schema('schema_name').all like the method name
this didnt work when i added this to the application record, but when i add to the specific model this worked
ok.... i figured it out!... this will work when added to application record.In my case the model is inherited from second database model (since i was using multiple database) i should add to record which it inherits from... Thank you again !!! :)
This was working for me when I added it in application record(id didn't even added it in individual models). There might be something different going on in your app. Of-course you have to adjust for your environment. @AbhishekAravindan

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.