2017-05-31 5 views
0

私はさまざまなattrタイプを持っている私のレールアプリでQuoteモデルを持っていますが、その中にはdbに送られているものもあれば、dbによって保存されているものもあれば、そうでないものもあります。ありがとうございました。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> 

[OK]をスタックトレースと@toddmethenyは私が不足しているかtypo'd許可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が上がりませんデータベースに保存されているのではなく、許可されたparamsです!何か案は?

奇妙な、psql select * from ..クエリは値が保存されていることを示していますが、Railsコンソールです。 Quote.last.payment_frequencyはnilを返します???

ああ、列挙型の列の型は整数でなければなりませんが、それは私のモデルの文字列ですが、おそらくこれが問題なのでしょうか?

enum'd attrsのデータ型が整数に変更され、すべてが期待どおりに動作します。

+0

更新された回答 – toddmetheny

答えて

1

スタックトレースを送信します。 co_numberは許可されたパラメータでホワイトリストに登録されていません。それは、その特定の分野の問題の少なくとも一部です。他のものは...ですが、ログに実際に表示されているものを投稿するので、フォームを通過しているものを確認できます。また、それらの値が保存されない理由についての手がかりを与えるメッセージがスタックトレースにあります。

更新:スタックトレースリスト2つの無許可パラメータ:co_numbercommission_level(あなたがコミッションレベルとco_numberを許可するタイプミスがない持っている)

物事があまりにも、ブランク値を持っているカップルのような... payment_frequencyindustry ...私はそれらのものが空でなければならない理由を掘り下げます。フォームにはこれらのものの値がありますか?彼らは渡されていません。それはあなたのゼロ価値の残りの部分を占めているようです。

+1

ここに1)typo'dと許容されないparamがありません.2)私がマップして書いたコードです。enumハッシュを人間化すると、enum値にマップされる元のenumキーが再現されませんでした。 3)db内のenum'd attrデータ型は文字列であり、整数である必要がありました。 – jbk

関連する問題