为什么某些模型属性没有保存在db中?

时间:2017-05-31 19:32:35

标签: ruby-on-rails model controller

我的rails应用程序中有一个Quote模型,它有各种attr类型,其中一些是由db发送/保存的,有些不是,我无法理解为什么。请帮助我理解,谢谢。

quotes_controller.rb

class QuotesController < ApplicationController

  def create
    @quote = Quote.new(quote_params)
    if @quote.save
        redirect_to root_url, notice: 'Quote request created'
    else
      render :new
    end
  end

private

  def quote_params
    params.require(:quote).permit(:gla, :prev_cover, :co_name, :postcode, :industry, :lives_overseas, 
                                  :scheme_start_date, :payment_frequency, :commision_level)
  end
end

quote.rb模型

class Quote < ApplicationRecord
        validates :gla, presence: { message: "Must be selected" }

        enum industry:          [ :financial_services, :architect, :business_consultancy ]
        enum payment_frequency: [ :annually, :monthly ]
end

schema.rb

create_table "quotes", force: :cascade do |t|
    t.boolean  "prev_cover"
    t.string   "co_name"
    t.integer  "co_number"
    t.string   "postcode"
    t.string   "industry"
    t.boolean  "lives_overseas"
    t.date     "scheme_start_date"
    t.string   "payment_frequency"
    t.integer  "commission_level"
    t.datetime "created_at",        null: false
    t.datetime "updated_at",        null: false
    t.boolean  "gla"
  end

rails console:

Pry> Quote.last.attributes
  Quote Load (0.4ms)  SELECT  "quotes".* FROM "quotes" ORDER BY "quotes"."id" DESC LIMIT $1  [["LIMIT", 1]]
=> {"id"=>6,
 "prev_cover"=>true,
 "co_name"=>"test1",
 "co_number"=>nil,
 "postcode"=>"al1 1aa",
 "industry"=>nil,
 "lives_overseas"=>true,
 "scheme_start_date"=>Wed, 31 May 2017,
 "payment_frequency"=>nil,
 "commission_level"=>nil,
 "created_at"=>Wed, 31 May 2017 19:23:07 UTC +00:00,
 "updated_at"=>Wed, 31 May 2017 19:23:07 UTC +00:00,
 "gla"=>true}

堆栈追踪:

Started POST "/quotes" for 127.0.0.1 at 2017-05-31 21:04:37 +0100
Processing by QuotesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"ILAo0Bs9Wq9lrVPlM2e6+a1kioV9zbni9Uxd5Yt/QSLNY3aVWyJ4TsEUmXN62RWgbueHksr/yN6avwEm8v7bEQ==", "quote"=>{"gla"=>"1", "prev_cover"=>"true", "co_name"=>"testing1", "co_number"=>"123456", "postcode"=>"al1 1aa", "industry"=>"", "lives_overseas"=>"true", "scheme_start_date(1i)"=>"2017", "scheme_start_date(2i)"=>"5", "scheme_start_date(3i)"=>"31", "payment_frequency"=>"", "commission_level"=>"10"}, "commit"=>"Get quote"}
Unpermitted parameters: co_number, commission_level
   (0.1ms)  BEGIN
  SQL (0.2ms)  INSERT INTO "quotes" ("prev_cover", "co_name", "postcode", "lives_overseas", "scheme_start_date", "created_at", "updated_at", "gla") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id"  [["prev_cover", "t"], ["co_name", "testing1"], ["postcode", "al1 1aa"], ["lives_overseas", "t"], ["scheme_start_date", "2017-05-31"], ["created_at", "2017-05-31 20:04:37.489368"], ["updated_at", "2017-05-31 20:04:37.489368"], ["gla", "t"]]
   (0.3ms)  COMMIT
Redirected to http://localhost:3000/
Completed 302 Found in 3ms (ActiveRecord: 0.6ms)


Started GET "/" for 127.0.0.1 at 2017-05-31 21:04:37 +0100
Processing by QuotesController#new as HTML
  Rendering quotes/new.html.erb within layouts/application
  Rendered quotes/new.html.erb within layouts/application (9.3ms)
Completed 200 OK in 34ms (Views: 32.7ms | ActiveRecord: 0.0ms)

来自Rails控制台。

[1] pry(main)> Quote.last
  Quote Load (0.2ms)  SELECT  "quotes".* FROM "quotes" ORDER BY "quotes"."id" DESC LIMIT $1  [["LIMIT", 1]]
=> #<Quote:0x007f951b14e918
 id: 7,
 prev_cover: true,
 co_name: "testing1",
 co_number: nil,
 postcode: "al1 1aa",
 industry: nil,
 lives_overseas: true,
 scheme_start_date: Wed, 31 May 2017,
 payment_frequency: nil,
 commission_level: nil,
 created_at: Wed, 31 May 2017 20:04:37 UTC +00:00,
 updated_at: Wed, 31 May 2017 20:04:37 UTC +00:00,
 gla: true>

好的堆栈跟踪和@toddmetheny帮助我理清丢失或错字的允许的attrs。现在只是枚举Quote.industriesQuote.payment_frequencies,其值不会被保存。

好的,所以代码改为;

这会从表单中发送attrs,但它们仍然没有在db,stack trace中创建:

Started POST "/quotes" for 127.0.0.1 at 2017-05-31 21:39:58 +0100
Processing by QuotesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"rkgX7CwrEHS/KnqG1C77mYkCiCEOWGshTxMCsbtGPdjGiDP20J4ccrAgplAGuKrdJyhECRWrsmXI0Ee9GNa6Zw==", "quote"=>{"gla"=>"1", "prev_cover"=>"true", "co_name"=>"halejulia", "co_number"=>"134532", "postcode"=>"al1 1aa", "industry"=>"financial_services", "lives_overseas"=>"true", "scheme_start_date(1i)"=>"2017", "scheme_start_date(2i)"=>"5", "scheme_start_date(3i)"=>"31", "payment_frequency"=>"monthly", "commission_level"=>"10"}, "commit"=>"Get quote"}
   (0.1ms)  BEGIN
  SQL (0.3ms)  INSERT INTO "quotes" ("prev_cover", "co_name", "co_number", "postcode", "industry", "lives_overseas", "scheme_start_date", "payment_frequency", "commission_level", "created_at", "updated_at", "gla") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) RETURNING "id"  [["prev_cover", "t"], ["co_name", "halejulia"], ["co_number", 134532], ["postcode", "al1 1aa"], ["industry", 0], ["lives_overseas", "t"], ["scheme_start_date", "2017-05-31"], ["payment_frequency", 1], ["commission_level", 10], ["created_at", "2017-05-31 20:39:58.957674"], ["updated_at", "2017-05-31 20:39:58.957674"], ["gla", "t"]]
   (0.3ms)  COMMIT
Redirected to http://localhost:3000/
Completed 302 Found in 3ms (ActiveRecord: 0.7ms)

rails console:

Quote.last.payment_frequency
  Quote Load (0.4ms)  SELECT  "quotes".* FROM "quotes" ORDER BY "quotes"."id" DESC LIMIT $1  [["LIMIT", 1]]
=> nil

所以仍然没有在数据库中保留params,它们被允许使用params!任何想法?

奇怪的是,psql select * from ..查询显示值已保存,但是Rails控制台; Quote.last.payment_frequency返回nil ???

啊哈,我看到对于枚举,列类型应该是整数,但它在我的模型中是字符串,这可能是问题吗?

enum'd attrs的数据类型更改为整数,并且所有数据类型都按预期运行。

1 个答案:

答案 0 :(得分:1)

发布堆栈跟踪。 scope: true未在允许的参数中列入白名单。这至少是该特定领域问题的一部分。其他人是...但是在日志中发布您实际看到的内容,以便我们可以看到通过表单传递的内容。堆栈跟踪中还会有消息,为您提供有关这些值未保存的原因的线索。

更新:堆栈跟踪列出了2个未经许可的参数:co_numberco_number(您有错误允许佣金级别且co_number不存在)

有些东西也有空白值......比如commission_levelpayment_frequency ......我会深入研究为什么这些东西不应该是空白的。表格是否具有这些东西的价值?他们没有被通过。这似乎可以解释其余的零值。