使用rails创建一个id数组

时间:2015-08-14 00:47:11

标签: ruby-on-rails arrays

我正在尝试创建一个id数组,以便我可以查询某个id是否与某个对象相关联。这是我的方法,但它不会在数组中添加id。

class Report
  include Mongoid::Document

  has_and_belongs_to_many :reportapprovals, class_name: "Reportapproval", inverse_of: :report

  def bind_reportapproval
    @reportapprovals = Reportapproval.where(tenant_id: self.tenant_id).all

    if @reportapprovals.present? && @reportapprovals.any? { |ra| ra.tenant_approved == true }
      @reportapprovals.each do |ra|
        self.reportapproval_ids = ra.id
      end
    end
  end
end

这是为了向report对象添加一个reportapproval_ids数组。

2 个答案:

答案 0 :(得分:1)

您将数组的值设置为等于一个ID。试试这个:

if true # use correct conditional statement
  self.reportapproval_ids = @reportapprovals.pluck(:id)
end

答案 1 :(得分:1)

Rails实际上有一个方法:

bar