【发布时间】:2016-02-03 19:08:46
【问题描述】:
我有什么可以从控制器调用模型中的方法,其中控制器具有活动记录关系对象
喜欢
@paid_drivers = DriverInvoice.where(driver_id: current_affilate_company.drivers.pluck(:id))
我在哪里
> <ActiveRecord::Relation [#<DriverInvoice id: 1, driver_id: 13, total_reservations: 2, total_from_reservations: 20.0, pay: 20.5,
> discount_amount: 0.0, other_amount: 0.5, tax: 0.0, created_at:
> "2016-02-03 05:14:56", updated_at: "2016-02-03 05:14:56",
> payment_type: 1, is_other_addition: true, is_tax_per: true, tax_value:
> nil>, #<DriverInvoice id: 2, driver_id: 56, total_reservations: 3,
> total_from_reservations: 452.0, pay: 452.0, discount_amount: 0.0,
> other_amount: 0.0, tax: 0.0, created_at: "2016-02-03 05:15:36",
> updated_at: "2016-02-03 05:15:36", payment_type: 1, is_other_addition:
> true, is_tax_per: true, tax_value: nil>]>
现在在那个 obj 上。我想要一个方法调用 @paid_drivers = @paid_drivers.search_paid_driver()
在我的模型中
def self.search_paid_driver()
raise self.inspect
end
只给出表格字段
喜欢
DriverInvoice(id: integer, driver_id: integer, total_reservations: 整数,total_from_reservations:浮动,支付:浮动,折扣金额: 浮动,其他金额:浮动,税:浮动,created_at:日期时间, updated_at:日期时间,payment_type:整数,is_other_addition: boolean, is_tax_per: boolean, tax_value: float)
我想要@paid_drivers 的所有记录,以便我可以在我的模型中进一步处理它们。
请问我该怎么做。
【问题讨论】:
-
是否要将所有付费驱动程序加载到内存中?只需
@paid_drivers = DriverInvoice.where(driver_id: current_affilate_company.drivers.pluck(:id)).all。但是你应该让它延迟加载 -
@LongNuyen 你能详细说明你的答案吗
-
但是你想要什么?
-
我想将更多过滤器放在我作为@paid_driver 获得的对象上
-
啊,你可以用
scope。这是文档api.rubyonrails.org/classes/ActiveRecord/Scoping/Named/…
标签: ruby-on-rails ruby ruby-on-rails-4 activerecord