# Exclude a column in save!

**URL:** <https://www.ruby-forum.com/t/exclude-a-column-in-save/99897>\
**Category:** Rails\
**Created:** [February 1, 2006, 10:24pm UTC](https://www.ruby-forum.com/t/exclude-a-column-in-save/99897 "2006-02-01T22:24:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rolando\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d9b06d/32.png) [@Rolando\_A](https://www.ruby-forum.com/u/Rolando_A)\
**Post date:** [February 1, 2006, 10:24pm UTC](https://www.ruby-forum.com/t/exclude-a-column-in-save/99897/1 "2006-02-01T22:24:09Z")

</div>

Hi all,  
I have a column that peeks its value from a sequence (postgres) some  
times. Other times, I set it to a certain value.  
This column has a default value of “nextval(‘the\_sequence’)”… When I  
want that this column peeks the value from the sequence, I do this:

obj = Mymodel.new  
obj.some\_column1 = blah  
obj.some\_column2 = blah  
obj.some\_column3 = blah  
obj.save!

but when looking at the logs, this creates a sql statement like this:

INSERT INTO Mymodels (some\_column1, some\_column2, some\_column3,  
problem\_column) VALUES (blah, blah, blah, NULL);

this is a problem, because it’s passing NULL and it’s not calling the  
default value… the only solution I see here is to create a trigger for  
insertion, that when the value is NULL set it to the next value from the  
sequence…  
what’s the rails-way to do this?

thanks!  
rolando.-

---

<div class="post-metadata">

**Author:** ![Rolando\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d9b06d/32.png) [@Rolando\_A](https://www.ruby-forum.com/u/Rolando_A)\
**Post date:** [February 1, 2006, 11:22pm UTC](https://www.ruby-forum.com/t/exclude-a-column-in-save/99897/2 "2006-02-01T23:22:04Z")

</div>

are you sure the column has something besides “DEFAULT NULL” in the  
create table syntax ?

THis seems odd. I think the save should not do this, am I wrong here ?

---

<div class="post-metadata">

**Author:** ![Rolando\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d9b06d/32.png) [@Rolando\_A](https://www.ruby-forum.com/u/Rolando_A)\
**Post date:** [February 2, 2006, 12:02am UTC](https://www.ruby-forum.com/t/exclude-a-column-in-save/99897/3 "2006-02-02T00:02:00Z")

</div>

Adam D. wrote:

> are you sure the column has something besides “DEFAULT NULL” in the  
> create table syntax ?

of course… the create table statement it’s like this:

CREATE TABLE movimientos (  
…  
num\_documento int DEFAULT nextval(‘num\_documento\_seq’),  
…  
);

regards,

---

<div class="post-metadata">

**Author:** ![Rolando\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d9b06d/32.png) [@Rolando\_A](https://www.ruby-forum.com/u/Rolando_A)\
**Post date:** [September 22, 2006, 9:12am UTC](https://www.ruby-forum.com/t/exclude-a-column-in-save/99897/4 "2006-09-22T09:12:18Z")

</div>

I have done a little work to help solve this problem

It may have been solved elsewhere

There may be a better way - interceding before the ‘INSERT’ sql statment  
to delete all ‘NULL’ valued fields that have DEFAULTs.

The way I have solved it is to specifically disable a NULL or nil column  
attribute by altering the Base.attributes function.

Try this: \<\<END\_OF\_CODE

module ActiveRecord  
module Defaults #:nodoc:  
def self.append\_features(base)  
super  
base.extend(ClassMethods)  
end  
module ClassMethods  
def default\_on\_null(\*options)  
class\_eval \<\<-EOV  
include ActiveRecord::Defaults::InstanceMethods  
def default\_on\_null  
#{options.inspect}  
end

```
			EOV
		end
	end
	module InstanceMethods
		public
		def attributes(options=nil)
			attributes=super(options)
			if d=default_on_null
				d.each do |de|
					unless attributes[de]
						print "warning: no attribute: #{de}\n" if 

```

!attributes.member?(de.to\_s)  
dt=attributes.delete(de.to\_s)  
end  
end  
end  
attributes  
end  
end  
end  
end

\<\<END\_OF\_CODE

Put it in your ActiveRecord lib directory and add to active\_record.rb :

At the end of requires:  
require ‘active\_record/defaults’

At the end of the class\_eval  
include ActiveRecord::Defaults

Cheers

---

<div class="post-metadata">

**Author:** ![Rolando\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d9b06d/32.png) [@Rolando\_A](https://www.ruby-forum.com/u/Rolando_A)\
**Post date:** [September 22, 2006, 9:20am UTC](https://www.ruby-forum.com/t/exclude-a-column-in-save/99897/5 "2006-09-22T09:20:17Z")

</div>

Also …

add default\_on\_null to your models where needed:

class SubMenu \< ActiveRecord::Base  
belongs\_to :main\_menu  
default\_on\_null :sposition,:field2  
end

This will allow these fields ( as attributes ) to actually use their  
default values when the attributes are nil

Cheers
