【问题标题】:How to create an instance method based on this object data?如何基于此对象数据创建实例方法?
【发布时间】:2011-08-19 03:19:48
【问题描述】:

我在这个变量中有一些数据,我想知道如何编写一个方法来给我商品价格的总和。

模型名称及其关联:

Order
- belongs_to :customer
- has_many :item_orders
- has_many :items, :through => :item_orders
- accepts_nested_attributes_for :item_orders, :allow_destroy => true, :reject_if => lambda { |a| a['item_id'].blank? }


Item
- has_many :item_orders, :dependent => :destroy
- has_many :orders, :through => :item_orders
- accepts_nested_attributes_for :item_orders, :allow_destroy => true

ItemOrder
- belongs_to :item
- belongs_to :order

在做:

<%= debug @order.items %> 

# returns me

- !ruby/object:Item
  attributes:
  id: 31
  title: Golden Ring 2
  price: 13445.0
  labour_cost: 500.0
  item_type_id: 10
  created_at: 2011-08-13 10:53:24.000000000Z
  updated_at: 2011-08-18 06:10:36.000000000Z
  photo: golden_ring.jpg
  changed_attributes: {}
  previously_changed: {}
  attributes_cache: {}
  marked_for_destruction: false
  destroyed: false
  readonly: false
  new_record: false
- !ruby/object:Item
 attributes:
  id: 32
  title: Special Pendant
  price: 171.67
  labour_cost: 120.0
  item_type_id: 20
  created_at: 2011-08-13 11:09:43.000000000Z
  updated_at: 2011-08-14 06:03:02.000000000Z
  photo: prd_194_48cd9b21771dd.jpg
  changed_attributes: {}
  previously_changed: {}
  attributes_cache: {}
  marked_for_destruction: false
  destroyed: false
  readonly: false
  new_record: false

所以,我想在 Order 模型类中编写一个方法,以便在视图上写入类似这样的内容。

 <%= @order.items.total_price %>

total_price 方法将对所有商品价格求和并乘以 @order.items 中包含的数量。

我的疑问是:从方法内部,我怎么能访问items 集合来执行求和操作。

def total_price
  self.item_order.each do |i| { 
     result += i.quantity * i.items.price
  }
end

编辑:我完全忘了提到我还必须考虑数量才能做出这个数学。

【问题讨论】:

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


    【解决方案1】:

    像这样在 order.rb 中定义一个函数:

    def total_price
      self.items.inject(0) {|sum, item| sum + item.price }
    end
    

    在视图中:

    <%= @order.total_price %>
    

    【讨论】:

      【解决方案2】:

      您有两种可能的方法。

      1) 使用 ActiveRecord sum。这是有效的,因为 SUM 操作是在 DB 中执行的。当您处理 100 行时,这是要走的路:

      items.sum(:price)  # executes select sum(items.price) 
                         #          from items where items.order_id = some_id
      

      2) 使用枚举sum。在这里,您正在对现有数组进行操作。

      items.to_a.sum(&:price)
      

      当我的has_many association 列表大小很小(

      修改 1:基于附加要求

      使用 ActiveRecord:

      item_orders.sum("item_orders.quantity * item.price", :join => :item)
      

      使用枚举

      item_orders.all(:include => :item).sum {|io| io.quantity * io.item.price }
      

      【讨论】:

      • 更新了答案。看看吧。
      • 我已经测试过了,它可以工作。确保您使用正确的关联名称。不清楚订单项关联的名称是item_order 还是order_items。你遇到了什么错误?
      • 我把它命名错了,所以它是item_orderquantity 属性属于order 表,其他属性属于item 表。
      • 你能提供模型名称和它们的关联吗?
      • 我已经更新了答案。假设您在 item_orders 表中有 quantity 列,它应该可以工作。
      【解决方案3】:

      &lt;%= @order.items.inject(0) {|sum, item| sum + item.price} %&gt;

      【讨论】:

      • 谢谢!尽管这可行,但我真的很想知道如何实现该方法。 :-)
      【解决方案4】:

      由于您的 Order 模型中有 has_many :items,您应该能够轻松地使用该 items 变量。你需要做的就是在你的 Order 模型中写下这个:

      def total_price
        items.to_a.sum {|item| item.price}
      end
      

      【讨论】:

        【解决方案5】:

        解决了。

        在我的order.rb 中,我创建了total_price 方法。

        def total_price
          self.item_orders.to_a.sum { |item| item.full_price }
        end
        

        然后,在我的item_order.rb

        def full_price
          quantity * self.item.price
        end
        

        这可以满足我的需要。谢谢大家!

        【讨论】:

          【解决方案6】:

          我很确定你可以这样做;

          def total_price
            items.sum(:price)
          end
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-09-22
            • 2022-01-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-05-24
            相关资源
            最近更新 更多