[英]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.