如何在ruby模型中实现以下查询:
select * from outsources where folder_id = 28 and id != 44 and returned = false;这里,如何访问当前的记录id而不是特定的id
以下是外包模型:
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时,我需要将状态从外包改为进行中
发布于 2015-10-09 15:00:58
看看ActiveRecord basics吧。在使用rails时,学习这一点至关重要。
对于您的查询,它将转换为:
Outsource.where(folder_id: 28, returned: false).where.not(id: 44)更新:
是的,你可以用你的变量替换这些数字。Rails甚至足够智能,可以自动获取属于current_folder的所有outsources。
current_folder.outsources.where(returned: false).where.not(id: current_outsource)https://stackoverflow.com/questions/33031770
复制相似问题