Dear all I have a dummy question. The model code as follow: class User < ActiveRecord::Base set_primary_key "username" end In script/console user = {:username => "123", :display_name => "345"} => {:username=>"123", :display_name=>"345"} a = User.new(user) => #<User username: nil, display_name: "345"> Why the username field is nil instead of "123"? I also tried {:id=>"123", :display_name=>"345"}, still not work But there is no problem when I do the following a.username = "123" or a.id = "123" a.save Please help. Thank you very much. Valentino -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 24 Feb 2009, at 14:00, Valentino Lun wrote:> > user = {:username => "123", :display_name => "345"} > => {:username=>"123", :display_name=>"345"} > > a = User.new(user) > => #<User username: nil, display_name: "345"> > > Why the username field is nil instead of "123"? I also tried > {:id=>"123", :display_name=>"345"}, still not work > > But there is no problem when I do the following > a.username = "123" or a.id = "123" > a.save > > Please help. Thank you very much. >Because the primary key is protected from mass assignment. Fred> Valentino > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On 24 Feb 2009, at 14:00, Valentino Lun wrote: >> But there is no problem when I do the following >> a.username = "123" or a.id = "123" >> a.save >> >> Please help. Thank you very much. >> > Because the primary key is protected from mass assignment. > > FredThank you for your reply Is that the only way to insert record like this? a = User.new a.username = "123" a.display_name = "456" a.save The code is not elegant then. In this situation, It is not allowed to use the scaffold code in controller like this? It is not convenient if the primary key is not the default "id" field. @user = User.new(params[:user]) Thanks Valentino -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
you just have to set the primary key manually. @user = User.new(params[:user]) @user.username = params[:username] @user.save however for security reasons i would not advise to use mass-assignment for user-creation anyways. there could be stuff like that in there: >> params[:user][:is_admin] => "1" --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Feb 24, 2009, at 9:13 AM, Valentino Lun wrote:> > Frederick Cheung wrote: >> On 24 Feb 2009, at 14:00, Valentino Lun wrote: >>> But there is no problem when I do the following >>> a.username = "123" or a.id = "123" >>> a.save >>> >>> Please help. Thank you very much. >>> >> Because the primary key is protected from mass assignment. >> >> Fred > > Thank you for your reply > > Is that the only way to insert record like this? > a = User.new > a.username = "123" > a.display_name = "456" > a.save > The code is not elegant then. > > In this situation, It is not allowed to use the scaffold code in > controller like this? It is not convenient if the primary key is not > the > default "id" field. > @user = User.new(params[:user]) > > Thanks > ValentinoNo, you can do this: a = User.new(:display_name => "456") do |u| u.id = 123 end a.save # or use .create in the first place rather than .new However, if you have ''username'' as the primary key, I''d strongly suggest that you reconsider that and follow the more conventional auto- incrementing integer primary key. Unless you have to deal with the structure of a legacy database, you''ll save yourself a lot of trouble. -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---