开发者

How could I pass a database run-time parameter in ruby on rails?

I need to set a run-time parameter in postgresql. It is "SET DATESTYLE TO PostgreSQL,E开发者_高级运维uropean;". Where can I pass that in a Rails Application?


An initializer would be an appropriate place to do that:

# config/initializers/set_datestyle.rb

ActiveRecord::Base.connection.execute 'SET DATESTYLE TO PostgreSQL,European;'


Don't you want the "SET DATASTYLE" statement for each new connection? Then an initializer isn't the right place.

I think you should take a look at

ActiveRecord::Base.postgresql_connection

or

ConnectionAdapters::PostgreSQLAdapter#configure_connection (private)

This ticket has a patch for a similar problem in the mysql world:

  • http://dev.rubyonrails.org/ticket/5260

Might help you look in the right files.

On the other hand, it may be easier to set the environment variable PGDATESTYLE, see

www.postgresql.org/docs/8.2/static/datatype-datetime.html

At the end of section "8.5.2. Date/Time Output" it says "The date/time styles can be selected by the user using the SET datestyle command, the DateStyle parameter in the postgresql.conf configuration file, or the PGDATESTYLE environment variable on the server or client. The formatting function to_char (see Section 9.8) is also available as a more flexible way to format the date/time output."

Either on the server, or the client.

Stephan


Have you tried ActiveRecord#execute ? It lets you send arbitrary sql to the database:

connection.execute("SET DATESTYLE TO PostgreSQL,European;")

I'm not sure if you need or want the semicolon there.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜