2017-05-31 77 views
0

我有一个报价模型在我的Rails应用程序中有各种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 

轨控制台:

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,但他们仍然没有在数据库生成,堆栈跟踪:

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) 

轨控制台:

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

所以仍然是PARAMS AREN在数据库中没有被持久化,他们可以参数化!有任何想法吗?

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

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

enum'd attrs的数据类型已更改为整数,并且所有行为与预期相同。

+0

更新回答下面 – toddmetheny

回答

1

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

更新:堆栈跟踪列表(2个)未经许可参数:co_numbercommission_level(你有一个错字允许的佣金水平,co_number是不存在的)

一对夫妇的东西具有空值,太...像payment_frequencyindustry ...我会深入探讨为什么这些东西如果不应该是空白的。表单是否有这些东西的价值?他们没有被通过。这似乎解释了你的无价值的其余部分。

+1

这里有三个问题1)一个错字和缺少允许的参数,2)我写的地图和代码。人性化枚举哈希不是重现原始枚举键映射到枚举值。 3)数据库中的enum'd attr数据类型是字符串,并且需要为整数。 – jbk

相关问题