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
> Valentino
No, 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
[email protected]
--~--~---------~--~----~------------~-------~--~----~
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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---