该Order模型:
class Order < ActiveRecord::Base
  has_many :sales, dependent: :destroy, inverse_of: :order
end
has_many Sales:
class Sale < ActiveRecord::Base
  belongs_to :order, inverse_of: :sales
  validates :order, :product, :product_group, :presence => true
  before_create :price
  def price
    mrr = Warehouse.where(:product => self.product).pluck(:mrr).shift.strip.sub(',', '.').to_f
    self.price = mrr * self.quantity.to_f
  end
end
当我销毁一个Order,相关的Sales也应该被销毁,但是这样做时我遇到了一个错误:
RuntimeError in OrdersController#destroy
Can't modify frozen hash
这一行突出显示:self.price = mrr * self.quantity.to_f.
Sale手动逐步销毁所有关联的记录可以正常工作而不会出错.在没有Sale关联之后,我也可以破坏Order记录.
有任何想法吗?
我怎样才能让它工作?
<%= form_for current_user, html: {multipart: true } do |f| %>
  <%= f.select(:brand, Brand.pluck(:company), {prompt:true}, {class: 'form-control'}, {:onchange => 'this.form.submit()'}) %>
<% end %>
目标是自动提交更改表单。但是上面的代码不起作用。我收到一个wrong number of arguments (5 for 1..4)错误。有任何想法吗?
我想在我的销售模型中验证销售折扣.
创建销售的表单从我的仓库模型接收产品数据并将其保存在销售记录中:
<%= f.select(:product, Warehouse.pluck(:product).uniq, {prompt:true}, {class: 'form-control'}) %>
Warehouse模型具有为此相应产品指定的折扣.现在我想检查是否sale.product等于warehouse.product,然后设置此次促销的折扣限额.那可能吗?像这样的东西:
validates_length_of :discount, maximum: Warehouse.where(:product => @sales.product).pluck(:discount), message: "Discount is to high"
提前谢谢了!
我有以下结构:一个Order has_many销售(和一个Sale belongs_to)Order。
quantity和 一个price列。totalprice栏totalprice由  属于父级的销售记录中的pricex quantityx组成。12Order
这是我的计算在订单模型中的样子:
class Order < ActiveRecord::Base
  has_many :sales, dependent: :destroy
  after_save :calc
  def price
   sales.to_a.sum { |item| item.price }
  end
  def totalpricex12
   totalpricex12 = price*12
  end
  def totaldiscount
   sales.to_a.sum { |item| item.discount }
  end
  def calc
   calc = (totalpricex12.to_f)-(totaldiscount.to_f)
   self.update_columns(totalprice: calc)
  end
end
问题是: totalprice只有在更新或保存时才会更新,但在添加或删除Order新内容时不会更新。Sale
我很好奇是否有一种将bash命令转换为ruby脚本的简单方法.
如果在命令行中键入此内容,则会获得ip网络列表.
whois -h whois.radb.net -- '-i origin AS35995' | grep -Eo "([0-9.]+){4}/[0-9]+"
是否有一种简单的方法可以在Ruby中执行类似的操作,或者我是否必须手动编写代码?
对不起noob问题,并提前感谢您的帮助!