首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ruby模型中的psql查询

ruby模型中的psql查询
EN

Stack Overflow用户
提问于 2015-10-09 14:54:38
回答 1查看 65关注 0票数 0

如何在ruby模型中实现以下查询:

代码语言:javascript
复制
select * from outsources where folder_id = 28 and id != 44 and returned = false;

这里,如何访问当前的记录id而不是特定的id

以下是外包模型:

代码语言:javascript
复制
class Outsource < ActiveRecord::Base
  include Workflow

  workflow_column :status

  # constants
  belongs_to :client
  belongs_to :vendor
  belongs_to :folder

  after_initialize :init_dates
  before_update :init_return_date

  validate :folder_availability, if: proc { |i| i.folder.present? }
  validates_presence_of :folder_id, :vendor_id

  scope :open_transactions, -> { where(status: [OUTSOURCED, FINISHED, CANCELED]) }

  workflow do
    state :outsourced
  end

  private

  def folder_availability
    errors.add(:folder_id, 'This folder is not available for outsource. Please review.') \
      if self.folder.completed? || self.folder.abandoned? || self.folder.canceled? || self.folder.closed?
  end

  def init_dates
    self.outsource_date ||= Date.today
  end

  def init_return_date
    self.return_date ||= Date.today
  end
end

这里的当前记录是Outsource和文件夹。其中同一个文件夹属于多个委外,当所有委外文件夹返回值均为true时,我需要将状态从外包改为进行中

EN

回答 1

Stack Overflow用户

发布于 2015-10-09 15:00:58

看看ActiveRecord basics吧。在使用rails时,学习这一点至关重要。

对于您的查询,它将转换为:

代码语言:javascript
复制
Outsource.where(folder_id: 28, returned: false).where.not(id: 44)

更新:

是的,你可以用你的变量替换这些数字。Rails甚至足够智能,可以自动获取属于current_folder的所有outsources

代码语言:javascript
复制
current_folder.outsources.where(returned: false).where.not(id: current_outsource)
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33031770

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档