# Insert into with only filfull fields

**URL:** <https://www.ruby-forum.com/t/insert-into-with-only-filfull-fields/125432>\
**Category:** Rails\
**Created:** [January 9, 2008, 1:44am UTC](https://www.ruby-forum.com/t/insert-into-with-only-filfull-fields/125432 "2008-01-09T01:44:06Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/49beb7/32.png) [@jney](https://www.ruby-forum.com/u/jney)\
**Post date:** [January 9, 2008, 1:44am UTC](https://www.ruby-forum.com/t/insert-into-with-only-filfull-fields/125432/1 "2008-01-09T01:44:06Z")

</div>

Hello,  
when i create a record, User.create(:login=\>‘john’, :password =\>  
‘password’), active record behaviour is to include every of my fields  
in the SQL query :  
INSERT INTO users (“login”, “password”, “another\_column”,  
“yet\_another\_column”) VALUES(‘john’, ‘password’, NULL, NULL)

But i want active record to just include filfulled fields in the  
query:  
INSERT INTO users (“login”, “password”) VALUES(‘john’, ‘password’)

how can i change it?

---

<div class="post-metadata">

**Author:** ![jney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/49beb7/32.png) [@jney](https://www.ruby-forum.com/u/jney)\
**Post date:** [January 9, 2008, 1:46am UTC](https://www.ruby-forum.com/t/insert-into-with-only-filfull-fields/125432/2 "2008-01-09T01:46:27Z")

</div>

Is there any reason you want to do this? This is ActiveRecord’s default  
behaviour and changing it would be a pain.

On Jan 9, 2008 11:13 AM, Jean-SÃ©bastien [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> INSERT INTO users (“login”, “password”) VALUES(‘john’, ‘password’)
> 
> how can i change it?
> 
> >

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

Feel free to add me to MSN and/or GTalk as this email.

---

<div class="post-metadata">

**Author:** ![jney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/49beb7/32.png) [@jney](https://www.ruby-forum.com/u/jney)\
**Post date:** [January 9, 2008, 1:51am UTC](https://www.ruby-forum.com/t/insert-into-with-only-filfull-fields/125432/3 "2008-01-09T01:51:43Z")

</div>

yes my database has some triggers: some fields are “NOT NULL” but  
triggers put the good values inside.

---

<div class="post-metadata">

**Author:** ![jney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/49beb7/32.png) [@jney](https://www.ruby-forum.com/u/jney)\
**Post date:** [January 9, 2008, 1:54am UTC](https://www.ruby-forum.com/t/insert-into-with-only-filfull-fields/125432/4 "2008-01-09T01:54:03Z")

</div>

You could move the triggers into the model, depending on what they do.

On Jan 9, 2008 11:21 AM, Jean-SÃ©bastien [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> wrote:
> 
> > > But i want active record to just include filfulled fields in the  
> > > query:  
> > > INSERT INTO users (“login”, “password”) VALUES(‘john’, ‘password’)
> > 
> > > how can i change it?
> > 
> > –  
> > Ryan B.[http://www.frozenplague.net](http://www.frozenplague.net)  
> > Feel free to add me to MSN and/or GTalk as this email.

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

Feel free to add me to MSN and/or GTalk as this email.

---

<div class="post-metadata">

**Author:** ![jney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/49beb7/32.png) [@jney](https://www.ruby-forum.com/u/jney)\
**Post date:** [January 9, 2008, 2:04am UTC](https://www.ruby-forum.com/t/insert-into-with-only-filfull-fields/125432/5 "2008-01-09T02:04:22Z")

</div>

Jean-sÃ©bastien Jney wrote:

> no i can’t: this database is not only acceed my rails processes, we  
> have some java tasks too.  
> I think the better way is to modify our triggers.

You could try adding this to your user model,

ignore\_column “another\_column”  
ignore\_column “yet\_another\_column”

after extending ActiveRecord::Base with

class ActiveRecord::Base  
def self.ignore\_column(column\_name)  
case column\_name  
when String, Symbol  
self.columns\_hash.delete(column\_name.to\_s)  
self.columns.delete\_if {|c| c.name == column\_name.to\_s}  
when Regexp  
self.columns\_hash.delete\_if { |k,v| column\_name.match(k)}  
self.columns.delete\_if { |c| column\_name.match(c.name)}  
end  
end  
end

Or do you need to read these columns as well?

Stephan

---

<div class="post-metadata">

**Author:** ![jney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/49beb7/32.png) [@jney](https://www.ruby-forum.com/u/jney)\
**Post date:** [January 9, 2008, 2:08am UTC](https://www.ruby-forum.com/t/insert-into-with-only-filfull-fields/125432/6 "2008-01-09T02:08:05Z")

</div>

yes i need to read them :(. I already use same kind of hack on other  
fields.

On Jan 9, 2:04 am, Stephan W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![jney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/49beb7/32.png) [@jney](https://www.ruby-forum.com/u/jney)\
**Post date:** [January 9, 2008, 1:59am UTC](https://www.ruby-forum.com/t/insert-into-with-only-filfull-fields/125432/7 "2008-01-09T01:59:54Z")

</div>

no i can’t: this database is not only acceed my rails processes, we  
have some java tasks too.  
I think the better way is to modify our triggers.

---

<div class="post-metadata">

**Author:** ![jney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/49beb7/32.png) [@jney](https://www.ruby-forum.com/u/jney)\
**Post date:** [January 9, 2008, 3:54am UTC](https://www.ruby-forum.com/t/insert-into-with-only-filfull-fields/125432/8 "2008-01-09T03:54:59Z")

</div>

Jean-sÃ©bastien Jney wrote:

> yes i need to read them :(. I already use same kind of hack on other  
> fields.
> 
> On Jan 9, 2:04 am, Stephan W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

In that case you can extend the User model with a UserWrite model, which  
has those columns hidden. The UserWrite model is only used by the  
controller for updates/inserts.

Stephan

---

<div class="post-metadata">

**Author:** ![jney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/49beb7/32.png) [@jney](https://www.ruby-forum.com/u/jney)\
**Post date:** [January 9, 2008, 7:39pm UTC](https://www.ruby-forum.com/t/insert-into-with-only-filfull-fields/125432/9 "2008-01-09T19:39:25Z")

</div>

ok, i’ll try your solution, thanks everyone for so quick answers

On 9 jan, 03:54, Stephan W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
