On Feb 14, 12:20 am, Gaspard Bucher <[email protected]> wrote:
> It seems this kind of code incites the use of database specific SQL
> all over the place. Moving these default settings in a before_save and
> extending the connection adapters (in a /lib/adapters_ext/... folder)
> to get the values seems like a cleaner way. There can also be
> transactional issues with this kind of code so it should be clearly
> stated as *exceptional*.
well that is indeed true - but sometimes you absolutely need this
functionaliy. currently, with ar, you cannot even save a record to a
postgresql field which is both non-null and has no default value. ar
will try to insert 'NULL' into the column and blows up. you
absolutely need to be able to pass in DEFAULT at minimum - which
requires non-quoting and non-typecasting of the value.
also your approach is impossible to provide data integrity with. this
is a bug
transaction do
record.created_at = Time.now
end
it's not obivous why until you do this
transaction do
10000.times do
record = Record.new
record.created_at = Time.now
end
end
now all records will have slightly different times. of course
databases have solved this for 20 years: the correct approach is
transaction do
10000.times do
record = Record.new
record.created_at = SQL('current_timestamp')
end
end
currently the only fix is use, guess what, database specific sql in
the create migration, but this is also another subtle bug (failing
silently but doing the wrong thing)
create_table :foo do |t|
t.created_at :timestamp, :default => Time.now
end
it's turtles all the way down - unless you use database functions you
cannot retrieve database values. you can do super hacky stuff like
now = connection.raw_connection.execute('select
current_timestamp').first[0]
but it's really a mess.
anyhow - the issue i was solving with this is the 'not null but no
default' issue which, imho, should be simple with something as large
as ar. it's simply unrealistic and non-pragmatic to *never* be able
to do anything literal with ar.
this post from a while ago
http://drawohara.com/post/6677354/rails-activerecord-default-values
while now dated (doesn't work with current ar) is the most read on my
blog: 30000 views, and i still get about 1 email per week regarding
it, so i think this is a real problem. i know if had to hack ar to do
something similar for every single rails app i've ever built, although
it is the case that i deal with legacy dbs more that most people.
kind regards.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Core" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-core?hl=en
-~----------~----~----~----~------~----~------~--~---