合并activerecord模型对象,并只保留一个created_at列

时间:2014-03-23 04:15:07

标签: ruby-on-rails ruby ruby-on-rails-3 activerecord

我试图在两个activerecord :: relation对象上进行activerecord合并,但保留一个特定的created_at列。这是查询:

Subscriber.joins(:subscriptions).merge(subscriptions)

订阅是另一个activerecord关系。我想使用订阅关系中的created_at列,但activerecord默认从Subscriber获取created_at列。

修改

正在合并的订阅是此方法(返回activerecord :: relation)

def subscriptions
  @subscriptions ||= begin
    s = CampaignSubscription.where(:campaign_id => campaign.id).order(order_clause)
    s = s.where(:status => status)
    s = s.where(:email => email) if email.present?
    s = s.where("email ILIKE ?", "%#{q}%") if q.present?
    s
  end
end

1 个答案:

答案 0 :(得分:0)

这可能有点矫枉过正,但应该有效。我没有测试它必须应用工会,但它适用于集合。

Subscriber.all.select((Subscriber.column_names - ['created_at']).join(', ')).joins(:subscriptions).merge(subscriptions)