在表中创建与其他2个相关的行

时间:2017-06-14 23:50:13

标签: ruby ruby-on-rails-4

有模特:

class Usdzar < ActiveRecord::Base
    has_many :smoothings
    has_many :smoothvals, through: :smoothings
end

class Smoothval < ActiveRecord::Base
    has_many :smoothprices
    has_many :usdzars, through: :smoothings
end

class Smoothing < ActiveRecord::Base

    belongs_to :usdzar
    belongs_to :smoothval

    accepts_nested_attributes_for :smoothval
    accepts_nested_attributes_for :usdzar

    before_save :create_smoothings

    def create_smoothings
    smoothval = Smoothval.find(smoothval_id) 
    1.upto(Usdzar.count) do
      Smoothing.create!(
        usdzar_id => usdzar.id,
        smoothval_id => smoothval.id,
        smprice => 99
        )
    end
end

使用平滑控制器我是从视图操作设置smoothval的值然后从这个表中的条目想要在价格上的usdzar表中的每一行上执行计算。我想要输出到平滑表中的输出。然后,每个价格id应该以从fval和sval计算的smprice id结束。

我的方法只是为了让循环工作但它失败了。

迁移:

class CreateSmoothings < ActiveRecord::Migration
  def change
    create_table :smoothings do |t|
      t.decimal :smprice,   :precision => 8, :scale => 5
      t.references :usdzar, index: true, foreign_key: true
      t.references :smoothval, index: true, foreign_key: true

      t.timestamps null: false
    end
    add_index :smoothings, :smprice
  end
end

class CreateSmoothvals < ActiveRecord::Migration
  def change
    create_table :smoothvals do |t|
      t.decimal :fval, :precision => 2, :scale => 0
      t.decimal :sval, :precision => 2, :scale => 0
      t.string :name
      t.string :nickname

      t.timestamps null: false
    end
    add_index :smoothvals, :name
  end
end

class CreateUsdzars < ActiveRecord::Migration
  def change
    create_table :usdzars do |t|
      t.date :day
      t.decimal :price,   :precision => 8, :scale => 5
      t.decimal :delta,   :precision => 8, :scale => 5

      t.timestamps null: false
    end
    add_index :usdzars, :day
    add_index :usdzars, :price
  end
end

'localhost:3000 / smoothings / new'(我选择用于从DD列表执行计算的smoothval:

<%= form_for(@smoothing) do |f| %>
  <% if @smoothing.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@smoothing.errors.count, "error") %> prohibited this smoothing from being saved:</h2>
      <ul>
      <% @smoothing.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :smoothvals %><br>
    <%= f.collection_select :smoothval_id, Smoothval.all, :id, :name %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

提交时我得到:

undefined method `id' for nil:NilClass

      Smoothing.create!(
        usdzar_id => usdzar.id,
        smoothval_id => smoothval.id,
        smprice => 99
      )

我的强烈指责:

params.require(:smoothing).permit(:smprice, :usdzar_id, :smoothval_id, :usdzars_attributes => [:id, :price, :delta])

有人可以让我知道为什么这样做以及更好的方法是什么?

3 个答案:

答案 0 :(得分:0)

create_smoothings方法中,未定义变量usdzar。问题在于您的upto循环,将您的方法更改为:

def create_smoothings
 smoothval = Smoothval.find(smoothval_id) 
 Usdzar.all.each do |usdzar|
  Smoothing.create!(
    usdzar_id => usdzar.id,
    smoothval_id => smoothval.id,
    smprice => 99
    )
end

答案 1 :(得分:0)

我使用控制器的解决方案:我将把它移到模型但现在让它工作

#POST / smoothings   def create

@sv = Smoothval.find(smoothing_params[:smoothval_id])

Usdzar.all.each do |usdzar|
    Smoothing.create!(
    usdzar_id: usdzar.id,
    smoothval_id: @sv.id,
    smprice: 99
    )
end
redirect_to smoothings_url, notice: 'Smoothings successfully created.'

不使用嵌套的attributes_for

答案 2 :(得分:0)

在控制器中完成

#POST / smoothings   def create

sv = Smoothval.find(smoothing_params[:smoothval_id])
fval = sv.fval
sval = sv.sval



Usdzar.all.each do |usdzar|

    if usdzar.id == 1
      prevusdzar = usdzar.price
      prevfirstsmprice = usdzar.price
    else
      previd = (usdzar.id) - 1
      prevusdzar = Usdzar.find(previd).price
      prevfirstsmprice = Smoothing.find(previd).firstsmprice
    end

    firstsmprice = (((fval * usdzar.delta)/100) + prevusdzar)

    firstsmdelta = firstsmprice - prevfirstsmprice

    finalsmprice = (((sval * firstsmdelta)/100) + prevfirstsmprice)

    Smoothing.create!(
      usdzar_id: usdzar.id,
      smoothval_id: sv.id,
      firstsmprice: firstsmprice,
      firstsmdelta: firstsmdelta,
      finalsmprice: finalsmprice
    )
end


redirect_to smoothings_url, notice: 'Smoothings successfully created.'

如果我要将它添加到模型中,我会在保存之前使用哪些操作? 我没有完成计算所以它变得很长,很可能需要一些评论。