Ruby on rails id does not save

I know this contradicts Ruby on rails conventions, but my identifier for this table does not require auto-increment and I set it through the logic. However, it is not stored in the database. All retrieved stored values ​​are stored as null for the identifier.

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 <% form_for @probe do |f| %> <%= f.error_messages %> <p> <%= f.label "Site name" %><br /> <%= f.text_field :name %> </p> <p style="margin-left: 10%"> <%= f.label "Probe Key" %><br /> <%= f.text_field :id, :value => @token %> </p> <p style="margin-left: 20%"> <%= link_to "Back to List", probes_path %> <%= f.submit "Submit",:style => "margin-left: 75px;" %></p> <% end %> 

Is it possible? Or is there somewhere besides the new.html.erb file that I have to change / check?

+4
source share
3 answers

Field: id is not available for mass assignment. you need to install it manually using

@probe.id = params[:probe][:id]

in your controller code.

(It can also work if you add :id to the attr_accessible list, and in general, you should set attr_accessible for each model that is directly assigned by the mass of the form parameters, but without testing I'm not sure that it will work, you still have to manually install :id )

+5
source

From AdminMyServer to 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 
+1
source

I am sure that ActiveRecord instances ActiveRecord configured so that the id attribute cannot be set using mass assignment, which usually creates an object through this form. If you look in your logs, you can see a warning saying along these lines.

If you set the identifier on purpose, rather than using something like p = Probe.new(params[:probe]) , then you should be fine. For instance.

 p = Probe.new(params[:probe]) p.id = param[:probe][:id] 
0
source

All Articles