开发者

How to make ActiveRecord access a table from a dblink (correctly)?

开发者 https://www.devze.com 2023-03-27 06:30 出处:网络
I am developing a RoR project using an Oracle database. Recently I added a DBLink to another database and this works perfectly just from an SQL.

I am developing a RoR project using an Oracle database. Recently I added a DBLink to another database and this works perfectly just from an SQL. When I want to add the table to my class with the following code...

class ServerModel < ActiveRecord::Base 
  set_table_name "S985.S985_947_MODELS_VW@R985_A947.WORLD"
  set_primary_key "model_barcode"

  acts_as_reportable
  acts_as_entity
end

... I get this error:

NativeException: java.sql.SQLException: ORA-02084: database name is missing a component
: select * from (SELECT "S985"."S985_947_MODELS_VW"@"R985_A947"."WORLD".* FROM "S985"."S985_947_MODELS_VW"@"R985_A947"."WORLD"  ) where rownum <= 14

Of course this automatically used query does not work. It shouldn't select开发者_Python百科 "S985"."S985_947_MODELS_VW"@"R985_A947"."WORLD".* but just "S985"."S985_947_MODELS_VW".*, without duplicating the name of the dblink (@R985_947.WORLD).

Is this possible with the set_table_name method or any other ActiveRecord method?

The answer looks simple, but I cannot find the solution. Can anybody help me with this problem?

Thanks!


Unsure about tweaking Active Record to include the DB Link name, but what about creating a view of the remote table, something like:

create or replace view S985_947_MODELS_VW
as
select * from S985_947_MODELS_VW"@"R985_A947"."WORLD"

And then just accessing the view in the ROR application? Not sure if the view can be inserted, updated etc - you would need to check that out. I have a feeling a 'simple' view (ie one on a single table without group by etc) can be updated and inserted into.


Is establish_connection what you're looking for?

http://apidock.com/rails/ActiveRecord/Base/establish_connection/class

You can use this to set class-specific connections for Models you choose.

0

精彩评论

暂无评论...
验证码 换一张
取 消