ruby on rails id不保存
我知道这违反了ruby on rails惯例,但是这个表的id不需要自动递增,而是我通过逻辑设置它。 但是,它不会保存到数据库中。 保存的所有内容都是为id保存为null。
def self.up create_table :probes, :id => false do |t| t.string :id t.string :name t.integer :user_id t.boolean :online t.timestamps end end
@token %>
"margin-left: 75px;" %>
这有可能吗? 或者除了new.html.erb文件之外还有哪些地方我应该更改/检查?
对于质量分配,无法访问:id字段。 你需要手动设置它
@probe.id = params[:probe][:id]
在您的控制器代码中。
(如果你在你的attr_accessible
列表中添加:id
也可以工作,通常你应该为每个从表单参数直接批量分配的模型设置attr_accessible
,但是没有测试我不确定它是否会起作用,您可能仍需要手动设置:id
)
来自AdminmeyServer, url为https://stackoverflow.com/questions/517869/id-field-without-autoincrement-option-in- migration
#using a number create_table(:table_name, :id => false) do |t| t.integer :id, :options => 'PRIMARY KEY' end #using as string, like the question (why?) create_table(:table_name, :id => false) do |t| t.string :id, :options => 'PRIMARY KEY' end
我很确定ActiveRecord
实例已设置好,因此无法通过质量赋值来设置id
属性,这通常是通过这样的forms创建对象的方式。 如果您查看日志,您可能会看到一条警告,说明这些内容。
如果你专门设置id而不是使用类似p = Probe.new(params[:probe])
那么你应该没问题。 例如
p = Probe.new(params[:probe]) p.id = param[:probe][:id]