Dado
2006-Sep-13 04:26 UTC
How to avoid AR confusing way of handling default values for attributes w/ :null => false
How can I avoid AR confusing way of handling default values for
attributes w/ :null => false?
This issue came up before, but I could not find an answer that resolves
the issue, which I outline below:
First, I add a test table using migrations:
class AddTestTable < ActiveRecord::Migration
def self.up
create_table "users", :force => true do |t|
t.column "email", :string, :null => false
t.column "age", :integer, :null => false
end
end
def self.down
drop_table "users"
end
end
$ script/generate model User
$ script/console
Loading development environment.
>> User.new.email
=> "">> User.new.age
=> 0
I just dont understand why it defaults email to "" and age to 0 and
not
to nil. Nowhere did
I set the default email value to "" and the default age value to 0 in
my migration.
Now if I do $rake db:schema:dump here''s what the generated schema looks
like:
ActiveRecord::Schema.define(:version => 1) do
create_table "users", :force => true do |t|
t.column "email", :string, :default => "", :null
=> false
t.column "age", :integer, :default => 0, :null => false
end
end
All of a sudden there is a :default=>"" for email and a default 0
for
age
The new user would validate fine even if I set validates_presence_of
:email, :age in the model file. It even gets worst if one sets a
foreign key to :null => false, as it would default to 0!
Is there a way of overriding this confusing way that AR has to handle
default values???
Thanx,
Edoardo "Dado" Marcora
--~--~---------~--~----~------------~-------~--~----~
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
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk
-~----------~----~----~----~------~----~------~--~---
Dominique Plante
2006-Sep-13 06:40 UTC
Re: How to avoid AR confusing way of handling default values
Dear Dado:
Here is the migration script you might be looking for! I created a new
app, created a user model, used the following migration, did the rake
migrate, then when I typed User.new.email, I got a nil instead of the
""
you were getting.
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.column "email", :string, :null => false, :default => nil
t.column "age", :integer, :null => false, :default => nil
end
end
def self.down
drop_table :users
end
end
I guess there are some people that think nil is a reasonable starting
value, and others that believe the default should be initialized to a
value within the range of allowable inputs.... I guess there will always
be needs to do it either way...
Hope this helps!
Dominique
Dado wrote:> How can I avoid AR confusing way of handling default values for
> attributes w/ :null => false?
>
...>
> Is there a way of overriding this confusing way that AR has to handle
> default values???
>
> Thanx,
>
> Edoardo "Dado" Marcora
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
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
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk
-~----------~----~----~----~------~----~------~--~---
Thank you soooo much! Dado --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Hi Dominique, you strategy makes a lot of sense, but I tried it w/ the latest rails and it still overrides my defaults (even when set to nil) and I am still getting User.new.email to be "" and User.new.age to be 0... I don''t remember AR behaving this way in the past. Any help? Dado Dominique Plante wrote:> Dear Dado: > > Here is the migration script you might be looking for! I created a new > app, created a user model, used the following migration, did the rake > migrate, then when I typed User.new.email, I got a nil instead of the "" > you were getting. > > class CreateUsers < ActiveRecord::Migration > def self.up > create_table :users do |t| > t.column "email", :string, :null => false, :default => nil > t.column "age", :integer, :null => false, :default => nil > end > end > > def self.down > drop_table :users > end > end > > I guess there are some people that think nil is a reasonable starting > value, and others that believe the default should be initialized to a > value within the range of allowable inputs.... I guess there will always > be needs to do it either way... > > Hope this helps! > > Dominique > > Dado wrote: > > How can I avoid AR confusing way of handling default values for > > attributes w/ :null => false? > > > ... > > > > Is there a way of overriding this confusing way that AR has to handle > > default values??? > > > > Thanx, > > > > Edoardo "Dado" Marcora > > > -- > Posted via http://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---