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

前端 未结 8 1153
执笔经年
执笔经年 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:46

    Trying to think of a potentially more efficient way, but for now:

    hash = { :a => 'some', :b => 'string', :c => 'foo', :d => 'bar' }
    @something = Something.new
    @something.attributes = hash.reject{|k,v| !@something.attributes.keys.member?(k.to_s) }
    @something.save
    

提交回复
热议问题