Ruby on Rails,SQL Server order by和comparison

时间:2015-11-22 14:03:13

标签: ruby-on-rails sql-server ruby ruby-on-rails-4 activerecord

我遇到以下问题时遇到问题。我不知道为什么。

这是用户模型:

class UserProfile < ActiveRecord::Base

  self.table_name = "UserProfile"
  self.primary_key = "UserId"

  has_many :user_access_job_list, class_name: 'UserAccessJobList', foreign_key: 'UserId'
  has_many :job_tables, class_name: 'JobTable', through: :user_access_job_list
  has_many :order_histories, class_name: 'OrderHist', through: :job_tables
  has_many :order_hist_lines, class_name: 'OrderHistLine', through: :job_tables
  has_many :delivery_histories, class_name: 'DeliveryHist', through: :job_tables    

end

这是订单行项目历史记录模型:

class OrderHistLine < ActiveRecord::Base

  self.table_name = "OrderHistLine"
  self.primary_key = "WebLineId"

  belongs_to :job_table, class_name: 'JobTable', foreign_key: 'JobId'
  belongs_to :job_product, class_name: 'JobProduct', foreign_key: 'ItemId'
  belongs_to :order_hist, class_name: 'OrderHist', foreign_key: 'WebOrderId'
  has_many :delivery_histories, class_name: 'DeliveryHist', foreign_key: 'WebLineId'

end

我正在尝试按交货日期排序订单项目历史记录(数据库中使用的名称列是OrderHist表中的DlvDate)。

我试过了:

@ user.order_hist_lines.includes(:order_hist).order(:DlvDate)

但是它给了我

ActiveRecord::StatementInvalid: TinyTds::Error: Invalid column name 'DlvDate'.: EXEC sp_executesql N'SELECT [OrderHistLine].* FROM [OrderHistLine] INNER JOIN [JobTable] ON [OrderHistLine].[JobId] = [JobTable].[JobId] INNER JOIN [UserAccessJobList] ON [JobTable].[JobId] = [UserAccessJobList].[JobId] WHERE [UserAccessJobList].[UserId] = @0 ORDER BY [OrderHistLine].[DlvDate] ASC', N'@0 nvarchar(10)', @0 = N'LamCK'

此外,我需要在往返日期之间找到订单。我做了:

@user.order_hist_lines.includes(:order_hist).where("DlvDate < ?", to_date).where("DlvDate > ?", from_date)

这给了我同样的错误。

编辑:

我使用了spickermann的答案。它起作用,但由于某种原因缺少大量数据。

@user.order_hist_lines.includes(:order_hist).order('OrderHist.DlvDate DESC').count

EXEC sp_executesql N'SELECT COUNT(DISTINCT [OrderHistLine].[WebLineId]) FROM [OrderHistLine] LEFT OUTER JOIN [OrderHist] ON [OrderHist].[WebOrderId] = [OrderHistLine].[WebOrderId] INNER JOIN [JobTable] ON [OrderHistLine].[JobId] = [JobTable].[JobId] INNER JOIN [UserAccessJobList] ON [JobTable].[JobId] = [UserAccessJobList].[JobId] WHERE [UserAccessJobList].[UserId] = @0', N'@0 nvarchar(10)', @0 = N'LamCK'  [["UserId", "LamCK"]]

我得到9

@user.order_hist_lines.includes(:order_hist).count

EXEC sp_executesql N'SELECT COUNT(*) FROM [OrderHistLine] INNER JOIN [JobTable] ON [OrderHistLine].[JobId] = [JobTable].[JobId] INNER JOIN [UserAccessJobList] ON [JobTable].[JobId] = [UserAccessJobList].[JobId] WHERE [UserAccessJobList].[UserId] = @0', N'@0 nvarchar(10)', @0 = N'LamCK'  [["UserId", "LamCK"]]

@user.order_hist_lines.count

EXEC sp_executesql N'SELECT COUNT(*) FROM [OrderHistLine] INNER JOIN [JobTable] ON [OrderHistLine].[JobId] = [JobTable].[JobId] INNER JOIN [UserAccessJobList] ON [JobTable].[JobId] = [UserAccessJobList].[JobId] WHERE [UserAccessJobList].[UserId] = @0', N'@0 nvarchar(10)', @0 = N'LamCK'  [["UserId", "LamCK"]]

我得到2719

1 个答案:

答案 0 :(得分:1)

在连接表格时尤其需要非常精确,特别是表格名称不符合Rails`conventions。

我认为以下内容应该有效:

@user.order_hist_lines.includes(:order_hist).
  order('OrderHist.DlvDate')

其他查询相同:

@user.order_hist_lines.includes(:order_hist).
  where('OrderHist.DlvDate BETWEEN ? AND ?', from_date, to_date)