Hello, I''m working with a postgresql table CREATE TABLE elements ( id serial primary key, c1 text default f1(), ... cN text default fN() ); But if I use de lines @element = Element.new @element.save the values that element take are the nextval in the id field and nules in all the other fields. How can I get the defaults? (but without rewriting the funcitions f1,...,fN in the rails aplication, of course) Thaks -- Daniel H
Daniel Hernandez wrote:> Hello, > I''m working with a postgresql table > > CREATE TABLE elements ( > id serial primary key, > c1 text default f1(), > ... > cN text default fN() > ); > > But if I use de lines > > @element = Element.new > @element.save > > the values that element take are the nextval in the id field and nules > in all the other fields. > > How can I get the defaults? > (but without rewriting the funcitions f1,...,fN in the rails aplication,I don''t think ActiveRecord works very well with PG functions as defaults, especially in production mode, you might want to search the archives on that a little though... HTH, Jeroen
You might look at http://dev.rubyonrails.org/ticket/2259 -- I use this approach occasionally, my current version: module ActiveRecord class Base def self.new_default unless @default_sql_query || @no_defaults_sql sql_query = <<-end_sql select (pg_get_expr(adbin, adrelid) || ''::'' || format_type(atttypid, atttypmod) || '' as '' || quote_ident(attname))::text as def_v from pg_attribute left join pg_attrdef on (attrelid = adrelid and adnum=attnum) where attrelid = ''#{table_name}''::regclass and not attisdropped and attnum>0 and atthasdef end_sql res = connection.query(sql_query) @no_defaults_sql = res.empty? unless res.empty? @default_sql_query = (res.collect {|r| r[0]}).join('','') end end return self.new if @no_defaults_sql row = connection.query("SELECT #{@default_sql_query}")[0] data = self.new row.keys.each { |name| data[name] = row[name] } data end end end On 5/3/06, Daniel Hernandez <dhernand@dcc.uchile.cl> wrote:> Hello, > I''m working with a postgresql table > > CREATE TABLE elements ( > id serial primary key, > c1 text default f1(), > ... > cN text default fN() > ); > > But if I use de lines > > @element = Element.new > @element.save > > the values that element take are the nextval in the id field and nules in > all the other fields. > > How can I get the defaults? > (but without rewriting the funcitions f1,...,fN in the rails aplication, > of course) > > Thaks > -- > Daniel H > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
What do you think about doing this in a model? class GarageSaleItem default_value :price, 0.1 end A new GarageSaleItem (with nil for price) will cost 10 cents, the default value. If a price is set, it''ll use that price. If the record is saved and no price is yet set, 10 cents is saved for the price. You can do this in the schema, I guess, but can you do this easily? class Mixture serialize :color default_value :color, [255, 0, 0] end As you can see, all mixtures are red mixtures by default. And then this: class Movie default_value :rating, Proc.new {|m| m.genre.default_rating } end Clearly this doesn''t convincingly map to a real situation, but it''s clear what''s going on. Instead of using a literal value, we''re calling a proc. Symbols would work they same way they do with filters. Also, this: class MultiFaceted default_value [:foo, :bar], true end I have a little plugin for this and I''m using it, but is this a pattern people use? Is it even a reasonable pattern? Why or why not? -- Michael Daines http://www.mdaines.com
At first glance this looks neat. I normally have class methods for constructing the objects when needed or use the AR after_create callback. While this allows me to have different "default values" depending on the situation, your syntax is more succinct. I wouldn''t mind giving your plugin a go. Release your plugin into the wild world and let it fend for itself :) On Thursday, June 01, 2006, at 8:34 PM, Michael Daines wrote:>What do you think about doing this in a model? > > class GarageSaleItem > default_value :price, 0.1 > end > >A new GarageSaleItem (with nil for price) will cost 10 cents, the >default value. If a price is set, it''ll use that price. If the >record is saved and no price is yet set, 10 cents is saved for the >price. You can do this in the schema, I guess, but can you do this easily? > > class Mixture > serialize :color > default_value :color, [255, 0, 0] > end > >As you can see, all mixtures are red mixtures by default. > >And then this: > > class Movie > default_value :rating, Proc.new {|m| m.genre.default_rating } > end > >Clearly this doesn''t convincingly map to a real situation, but it''s >clear what''s going on. Instead of using a literal value, we''re >calling a proc. Symbols would work they same way they do with filters. > >Also, this: > > class MultiFaceted > default_value [:foo, :bar], true > end > >I have a little plugin for this and I''m using it, but is this a >pattern people use? Is it even a reasonable pattern? Why or why not? > > >-- >Michael Daines >http://www.mdaines.com >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/railsCheers! --Dave Teare http://1passwd.com - Password Manager + AutoFill for Mac OS X http://devlists.com - Email list management http://palmsphere.com - Apps for your hand-held -- Posted with http://DevLists.com. Sign up and save your mailbox.