简体   繁体   English

您可以使用Establishment_connection在Rails中连接到SQL视图吗

[英]Can you use establish_connection to connect to an SQL view in rails

I need to pull data from an SQL database belonging to Sage CRM. 我需要从属于Sage CRM的SQL数据库中提取数据。 I am able to connect and work with the data tables. 我能够连接并使用数据表。 I need to work with one of the views in the database and can't seem to get that connection. 我需要使用数据库中的一种视图,但似乎无法获得该连接。

Here is how I'm connecting to the tables. 这是我连接表格的方式。

class CrmTable < ActiveRecord::Base

      self.abstract_class = true
      def self.table_name_prefix
        'dbo_'
      end
   # establish_connection "crm_tables_#{Rails.env}"
      establish_connection "crm_tables"
end

class Address < CrmTable
  self.table_name = "Address"
end

class Opportunity < CrmTable
  self.table_name = "Opportunity"
end

That connects me to the Address and Opportunity tables with no issues. 这将我毫无问题地连接到“地址”和“机会”表。

However, I want to be able to access the SQL view of database/views/dbo.vPerson. 但是,我希望能够访问database / views / dbo.vPerson的SQL视图。

I tried variations on the following: 我尝试了以下变化:

class Vperson < CrmTable
  self.table_name = "Vperson"
end

However, I don't seem to be getting any records. 但是,我似乎没有任何记录。 I tried putting the following in my view but it was nil 我尝试将以下内容视为我的观点,但没有成功

<% @view_name = CrmTable::Vperson.all %>

<% @view_name.inspect %>

Is it possible to establish an active record connection to a view? 是否可以建立与视图的活动记录连接? If so, what am I doing wrong? 如果是这样,我在做什么错?

OK, I feel STUPID.. I forgot the = in the <% @view_name.inspect %> line.. It should be <%= @view_name.inspect %>. OK,我感觉很愚蠢。我在<%@ view_name.inspect%>行中忘记了=。它应该是<%= @ view_name.inspect%>。 That explains why I wasn't seeing anything.. 这就解释了为什么我什么都没看见。

I did see another question that mentioned a need to add a primary key when you are connecting to views. 我确实看到了另一个问题,它提到在连接视图时需要添加主键。

How can I use SQL Server Table Views as Rails Models (Read Only)? 如何将SQL Server表视图用作Rails模型(只读)?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails无法使用ActiveRecord :: establish_connection连接到数据库 - Rails cant connect to database with ActiveRecord::establish_connection Rails-建立连接和嵌套表单 - Rails - establish_connection and nested forms 测试和建立连接 - testing and establish_connection 无法打开rails console:生成数据库未配置,establish_connection引发ActiveRecord :: AdapterNotSpecified - Can't open rails console: production database not configured, establish_connection raises ActiveRecord::AdapterNotSpecified 用rails查询外部数据库。 抽象类+建立连接? - Query external database with rails. Abstract Class + establish_connection? Rails 6 和 Puma 是否仍需要在工作启动时建立连接? - Is establish_connection on worker boot still required on Rails 6 and Puma? 如何在Rails中与多个数据库并行建立连接? - How to establish_connection with more than one database in parallel in Rails? rails 3 Establishment_connection对切换数据库不起作用 - rails 3 establish_connection doesn't work for switching databases 建立连接和耙任务在一起的问题 - issue with establish_connection and rake task together 访问模型或表时建立连接 - establish_connection when accessing model or table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM