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

查看:59
本文介绍了如何将 SQL Server 表视图用作 Rails 模型(只读)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 SQL Server 作为我的 Rails 项目的数据库.我正在尝试创建一些模型以用于 3rd 方数据库,并且只想从该数据库中读取.所以我创建了一个我想为其创建对象的表的视图,然后我想将我的活动记录模型指向它.但是,在 Rails 控制台中,我没有得到预期的结果.返回一些正确信息的唯一示例是当我对对象执行 count 时,如下面的示例 3 所示.

I'm using SQL Server as my database for my Rails project. I'm trying to create some models to use for a 3rd party database and only want to read from this database. So I made a view of the table I wanted to create an object for and then I wanted to point my active record model to it. However, in rails console I don't get back expected results. The only example that gives back some correct information is when I do a count on the object as shown in Example 3 below.

我使用以下 gem 连接到我的 SQL Server:

I'm using the following gems to connect to my SQL Server:

gem 'tiny_tds'
gem 'activerecord-sqlserver-adapter'

我也安装了 freetds-dev 0.91-6build1

Also I have installed freetds-dev 0.91-6build1

示例 1

2.2.2 :004 > Game.all
  Game Load (268.7ms)  EXEC sp_executesql N'SELECT [games].* FROM [games]'
 => #<ActiveRecord::Relation [#<Game >, #<Game >, #<Game >, #<Game >, #<Game >, #<Game >, #<Game >, #<Game >, #<Game >, #<Game >, ...]> 

示例 2

2.2.2 :001 > Game.first
  SQL (1.1ms)  USE [Incoming]
  Game Load (1.8ms)  EXEC sp_executesql N'SELECT  [games].* FROM [games] OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY'
TinyTds::Error: Incorrect syntax near '0'.: EXEC sp_executesql N'SELECT  [games].* FROM [games] OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY'
ActiveRecord::StatementInvalid: TinyTds::Error: Incorrect syntax near '0'.: EXEC sp_executesql N'SELECT  [games].* FROM [games] OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY'
    from /home/daveomcd/.rvm/gems/ruby-2.2.2/gems/activerecord-sqlserver-adapter-4.2.3/lib/active_record/connection_adapters/sqlserver/database_statements.rb:336:in `each'
    ...
    ...

示例 3

2.2.2 :008 > Game.count
   (4.7ms)  EXEC sp_executesql N'SELECT COUNT(*) FROM [games]'
 => 12541 

incoming_model.rb

class IncomingModel < ActiveRecord::Base
  self.abstract_class = true
  self.table_name_prefix = "Incoming.dbo."
  establish_connection "incoming_#{Rails.env}".to_sym
end

game_model.rb

class Game < IncomingModel
  self.table_name = 'games'
end

database.yml

incoming_development:
  <<: *default
  adapter: sqlserver
  host: games-data
  port: 1433
  database: Incoming
  username: ****
  password: ****
  pool: 5
  timeout: 15000

推荐答案

于是我发现我需要为表指定一个主键.所以这是我添加的内容.

So I found that I needed to specify a primary key for the table. So here is the addition I made.

class Game < IncomingModel
  self.table_name = 'games'
  self.primary_key = 'game_id', 'this_other_column'
end

我还必须根据自己的需要合并 gem composite_primary_keys,但是,这可能比某些开发人员需要的要多.感谢所有花时间尝试帮助解决此问题的人!

I also had to incorporate for my needs the gem composite_primary_keys, however, that might be more than what some developers will need. Thanks to anyone that took the time to try and help troubleshoot this issue!

这篇关于如何将 SQL Server 表视图用作 Rails 模型(只读)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆