Ruby on rails 如何从每种产品中获得价值?
这里是Rails新手…。我使用下面的公式来获得第一个产品的速率值。我真正想要的是得到每种产品的税率值,以便计算每种产品的税收?有什么办法可以做到这一点吗Ruby on rails 如何从每种产品中获得价值?,ruby-on-rails,Ruby On Rails,这里是Rails新手…。我使用下面的公式来获得第一个产品的速率值。我真正想要的是得到每种产品的税率值,以便计算每种产品的税收?有什么办法可以做到这一点吗 product = Product.first product.tax_rate.rate` 谢谢 更新1 这就是我在订单模型中尝试做的: def tax_amount amount*rates end def rates product = Product.all.find_each do |product| produ
product = Product.first
product.tax_rate.rate`
谢谢
更新1
这就是我在订单模型中尝试做的:
def tax_amount
amount*rates
end
def rates
product = Product.all.find_each do |product|
product.tax_rate.rate
end
end
def amount
line_items.to_a.sum { |item| item.total_price }
end
def total
amount+tax_amount
end
结束
这些协会是:
class Product < ApplicationRecord
belongs_to :category
belongs_to :tax_category, :class_name => 'TaxCategory'
has_one :tax_rate, through: :tax_category, source: :tax_rate
has_one :zone, through: :tax_rate, source: :zone
class TaxCategory < ApplicationRecord
belongs_to :tax_rate
has_one :zone, through: :tax_rate, source: :zone
has_many :products
end
class TaxRate < ApplicationRecord
belongs_to :zone, :class_name => 'Zone'
has_many :tax_categories
end
更新2
现在我实现了这个:Product.joins:tax\u rate.pull'tax\u rates.rate'您有几种方法来实现您的目标。最直接的问题是:
Product.all.each do |product|
product.tax_rate.rate
# whatever
end
但是,如果数据库中包含大量产品实例,则会耗尽内存。在这种情况下,您可以做任何您想做的事情,使用以下内容批量加载它们:
您可以使用列名称。这相当于从表中简单地选择列名称
这对博士后有效。语法可能因您选择的数据库而异。如果您只需要获取所有产品的总税额,这应该适用于您:
def self.total_tax_amount(order = nil)
tax_amount = 0
if order.present?
order.products.find_each { |product| tax_amount += product.calculate_tax }
else
Product.all.find_each { |product| tax_amount += product.calculate_tax }
end
tax_amount
end
def amount
line_items.to_a.sum(&:total_price)
end
def calculate_tax
amount * tax_rate.rate
end
# This will give you the total amount for the entire products table:
Product.total_tax_amount
# This will give you the total tax amount for a specific order:
@order = Order.first
Product.total_tax_amount(@order)
更新1
听起来您与产品有订单关联。我修改了原始代码,如果不传递任何参数,则为您提供整个products表的总税额;如果向其传递Order对象,则只为您提供与该订单相关的产品的总税额。Product.all.each?但是,如果有很多产品,你可能不想这样做,这取决于你实际上在做什么。什么是线项目?我不明白你想得到什么。你只是想得到所有产品的所有税率的数组吗?@jeffdill2…我想得到每个产品的相关税率值,并计算总税额。我有一个税率表:name:string,rate:decimal,zone:references,它通过一个税种与每个产品关联。您可以查看上面的关联。这是3个不同的模型,有适当的关联。啊,你是说你的关联工作不正常?i、 产品税率不正常?谢谢。我在这个方法下得到了一个空白。我已经更新了问题,这样你就可以理解我想做什么了。我一直在想这个问题,所以我很感谢你的帮助!!谢谢您的回复@nicooga!!嗯,奇怪。。。现在我得到一个数组。我上传了一个快照,你可以看到。这行代码返回一个列表,其中列出了与产品相关的每个税率。嗯。。。现在它给了我一个未定义的方法total_tax_amount在这一行tax amount:重新启动Rails服务器。
Product.joins(:tax_rate).pluck('"tax_rates"."rate"')
def self.total_tax_amount(order = nil)
tax_amount = 0
if order.present?
order.products.find_each { |product| tax_amount += product.calculate_tax }
else
Product.all.find_each { |product| tax_amount += product.calculate_tax }
end
tax_amount
end
def amount
line_items.to_a.sum(&:total_price)
end
def calculate_tax
amount * tax_rate.rate
end
# This will give you the total amount for the entire products table:
Product.total_tax_amount
# This will give you the total tax amount for a specific order:
@order = Order.first
Product.total_tax_amount(@order)