# Cannot pass id field to ActiveRecord

**URL:** <https://www.ruby-forum.com/t/cannot-pass-id-field-to-activerecord/161231>\
**Category:** Rails\
**Created:** [February 24, 2009, 3:00pm UTC](https://www.ruby-forum.com/t/cannot-pass-id-field-to-activerecord/161231 "2009-02-24T15:00:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [February 24, 2009, 3:00pm UTC](https://www.ruby-forum.com/t/cannot-pass-id-field-to-activerecord/161231/1 "2009-02-24T15:00:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [February 24, 2009, 3:03pm UTC](https://www.ruby-forum.com/t/cannot-pass-id-field-to-activerecord/161231/2 "2009-02-24T15:03:15Z")

</div>

On 24 Feb 2009, at 14:00, Valentino L. 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

---

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [February 24, 2009, 3:13pm UTC](https://www.ruby-forum.com/t/cannot-pass-id-field-to-activerecord/161231/3 "2009-02-24T15:13:15Z")

</div>

Frederick C. wrote:

> On 24 Feb 2009, at 14:00, Valentino L. 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

---

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [February 24, 2009, 3:34pm UTC](https://www.ruby-forum.com/t/cannot-pass-id-field-to-activerecord/161231/4 "2009-02-24T15:34:01Z")

</div>

On Feb 24, 2009, at 9:13 AM, Valentino L. wrote:

> > 
> 
> 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 B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [February 24, 2009, 3:26pm UTC](https://www.ruby-forum.com/t/cannot-pass-id-field-to-activerecord/161231/5 "2009-02-24T15:26:53Z")

</div>

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”
