# Ignoring column on save

**URL:** <https://www.ruby-forum.com/t/ignoring-column-on-save/114944>\
**Category:** Rails\
**Created:** [September 19, 2007, 2:30am UTC](https://www.ruby-forum.com/t/ignoring-column-on-save/114944 "2007-09-19T02:30:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![andre123](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7ba0ec/32.png) [@andre123](https://www.ruby-forum.com/u/andre123)\
**Post date:** [September 19, 2007, 2:30am UTC](https://www.ruby-forum.com/t/ignoring-column-on-save/114944/1 "2007-09-19T02:30:19Z")

</div>

How can I tell Rails to completely ignore one specific column when  
INSERTing using the save method?  
I want the database’s default value to be used, but currently Rails is  
setting it to NULL.

---

<div class="post-metadata">

**Author:** ![andre123](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7ba0ec/32.png) [@andre123](https://www.ruby-forum.com/u/andre123)\
**Post date:** [September 19, 2007, 9:29am UTC](https://www.ruby-forum.com/t/ignoring-column-on-save/114944/2 "2007-09-19T09:29:54Z")

</div>

Andre S. wrote:

> How can I tell Rails to completely ignore one specific column when  
> INSERTing using the save method?  
> I want the database’s default value to be used, but currently Rails is  
> setting it to NULL.

I don’t believe you can tell Rails to ignore a column on a save (I don’t  
see any such options in the API docs).

When you do a \<Model\_name\>.new it’ll put in any default values for the  
attributes at that time. If you don’t alter those attributes it should  
still have the default values in there.

Are you altering the attributes in some way but want to reset them back  
to their defaults? If so I believe the only way to “reset” them is to  
set the values of those attributes manually (having saved off the  
default values before they were altered somewhere).

–  
Michael W.

---

<div class="post-metadata">

**Author:** ![andre123](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7ba0ec/32.png) [@andre123](https://www.ruby-forum.com/u/andre123)\
**Post date:** [September 19, 2007, 10:42am UTC](https://www.ruby-forum.com/t/ignoring-column-on-save/114944/3 "2007-09-19T10:42:10Z")

</div>

On Sep 19, 2007, at 2:29 AM, Andre S. wrote:

> How can I tell Rails to completely ignore one specific column when  
> INSERTing using the save method?  
> I want the database’s default value to be used, but currently Rails is  
> setting it to NULL.

Rails sets the database default value when you create a new instance  
of the object, that’s done by attributes\_from\_column\_definition

# Initializes the attributes array with keys matching the columns

from the linked table and

# the values matching the corresponding default value of that

column, so

# that a new instance, or one populated from a passed-in Hash,

still has all the attributes

# that instances loaded from the database would.

def attributes\_from\_column\_definition  
self.class.columns.inject({}) do |attributes, column|  
attributes[column.name] = column.default unless column.name ==  
self.class.primary\_key  
attributes  
end  
end

which in turn is called by AR::Base.new. Is your code assigning nils  
by hand?

– fxn

---

<div class="post-metadata">

**Author:** ![andre123](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7ba0ec/32.png) [@andre123](https://www.ruby-forum.com/u/andre123)\
**Post date:** [September 19, 2007, 1:41pm UTC](https://www.ruby-forum.com/t/ignoring-column-on-save/114944/4 "2007-09-19T13:41:45Z")

</div>

On 9/19/07, Xavier N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> self.class.primary\_key  
> attributes  
> end  
> end
> 
> which in turn is called by AR::Base.new. Is your code assigning nils  
> by hand?

This is the column, database is PostgreSQL:

codigo BIGINT UNIQUE NOT NULL DEFAULT nextval(‘customers\_codigo\_seq’),

It’s like an auto-incrementing column in MySQL.

Currently, I’m not setting it to anything, Rails is setting it to  
NULL, and PostgreSQL complains that:

null value in column “codigo” violates not-null constraint : INSERT  
INTO customers …

It needs to be DEFAULT, without quotes, or not sent at all.

Thank you!

---

<div class="post-metadata">

**Author:** ![andre123](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7ba0ec/32.png) [@andre123](https://www.ruby-forum.com/u/andre123)\
**Post date:** [September 20, 2007, 1:46am UTC](https://www.ruby-forum.com/t/ignoring-column-on-save/114944/5 "2007-09-20T01:46:46Z")

</div>

On 9/19/07, Andre S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > of the object, that’s done by attributes\_from\_column\_definition  
> > attributes[column.name] = column.default unless column.name ==  
> > codigo BIGINT UNIQUE NOT NULL DEFAULT nextval(‘customers\_codigo\_seq’),
> 
> It’s like an auto-incrementing column in MySQL.
> 
> Currently, I’m not setting it to anything, Rails is setting it to  
> NULL, and PostgreSQL complains that:
> 
> null value in column “codigo” violates not-null constraint : INSERT  
> INTO customers …
> 
> It needs to be DEFAULT, without quotes, or not sent at all.

I wrote a small stored procedure that sets the codigo column when it’s  
NULL on INSERT. There’s probably a much better way to solve this  
problem, but I haven’t figured it out yet. Any ideas?

Anybody using sequences with PostgreSQL?

---

<div class="post-metadata">

**Author:** ![andre123](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7ba0ec/32.png) [@andre123](https://www.ruby-forum.com/u/andre123)\
**Post date:** [September 27, 2007, 6:14pm UTC](https://www.ruby-forum.com/t/ignoring-column-on-save/114944/6 "2007-09-27T18:14:20Z")

</div>

Andre S. wrote:

> > > self.class.primary\_key  
> > > It’s like an auto-incrementing column in MySQL.  
> > > NULL on INSERT. There’s probably a much better way to solve this  
> > > problem, but I haven’t figured it out yet. Any ideas?

Unfortunately, Rails can’t sense default values when they’re result of a  
function. What it does is merely parse the the text metadata for default  
value. So in your case the workarounds are either stored procedure (what  
you did) or PostgreSQL rules on INSERT/UPDATE.

–  
Sava C.
