Rails: Ignoring non-existant attributes passed to create()

前端 未结 8 1159
执笔经年
执笔经年 2021-02-03 22:12

I have the following Rails model:

class CreateFoo < ActiveRecord::Migration
  def self.up
    create_table :foo do |t|
      t.string :a
      t.string :b
            


        
8条回答
  •  逝去的感伤
    2021-02-03 22:19

    You can use Hash#slice and column_names method exists also as class method.

    hash = {a: 'some', b: 'string', c: 'foo', d: 'bar'}
    Foo.create(hash.slice(*Foo.column_names.map(&:to_sym)))
    

提交回复
热议问题