Overriding id on create in ActiveRecord

后端 未结 13 2614
自闭症患者
自闭症患者 2020-11-28 05:07

Is there any way of overriding a model\'s id value on create? Something like:

Post.create(:id => 10, :title => \'Test\')

would be ide

13条回答
  •  被撕碎了的回忆
    2020-11-28 06:10

    we can override attributes_protected_by_default

    class Example < ActiveRecord::Base
        def self.attributes_protected_by_default
            # default is ["id", "type"]
            ["type"]
        end
    end
    
    e = Example.new(:id => 10000)
    

提交回复
热议问题