{S.A.Z.W.Q.A}

Entries categorized as ‘Rails’

How to find different environment configurations in rails

October 10, 2008 · 1 Comment

While working with a multiple-database application I came across the point where I had to juggle between different environment configurations.

I had to pass connection hash to ActiveRecord::Base.establish_connection, something like this:

ActiveRecord::Base.establish_connection
(
  :adapter=>"mysql",
  :host=>"localhost",
  :username=>"myuser",
  :password=>"mypass",
  :database => "somedatabase"
)

This connection can be easily retrieved from ActiveRecord::Base.configurations, this is currently undocumented.

Using ActiveRecord::Base.configurations, the above can be simply written as:

ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['production'])

Categories: Rails · activerecord · multiple-models · rails-environments · rubyonrails
Tagged: ,

Defining customized Date / Time format in Rails

February 18, 2008 · 1 Comment

Generally to display customized Date / Time in rails we use helper method like:

# formats the date
def show_date(date)
  date.strftime("%B %d, %Y")
end

But there is another better way to do this, injecting customized rules in respective DATE_FORMATS hash of ActiveSupport::CoreExtensions module.

# define this in your environment.rb
# Default date/time format
ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge!(:standard => "%B %d, %Y")
ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(:standard => "%B %d, %Y")

Using it is simple:

Date.today.to_s(:standard)
Time.now.to_s(:standard)

Categories: ActiveSupport · Rails
Tagged:

mongrel, RMagick breaks with rubygems 0.9.5 on windows

December 21, 2007 · Leave a Comment

To upgrade to rails 2.0.2 one must upgrade to rubygems 0.95 but instead of fixing something up it breaks a lot of existing stuff like mongrel and RMagick etc.

To fix your broken gems:

  1. Download rubygems 0.94
  2. Extract them to C:\ruby\lib\ruby\gems\1.8\gems
  3. Run setup.rb

This will fix your broken gems.

Categories: Rails · rubygems

ActiveRecord : an interesting observation

September 15, 2007 · Leave a Comment

While working with ActiveRecord I observed something interesting, worth sharing.

Say we have the relation:

author has_many books

Now I create a new author say ‘a’ and a new book say ‘b’

a = Author.new
b = Book.new

Now, If I have a piece of code where I push ‘b’ into ‘a’ without saving ‘a’ first, what should happen ?

Like I do it like this:

a.books << b

see that neither ‘a’ nor ‘b’ has been saved, so what should happen? Should it give an error quoting nil id in author table or something else ?

Well things turned out to be different rather, when we push ‘b’ into ‘a’ without saving ‘a’ first, it gets pushed !

Yes, you heard it, it gets pushed.

Now everybody will wonder that how is it possible, without any id in scenario till now how the actual relationship can be handled internally ? without any id or book_id how can two records inference them ?

Well I suppose (yeah it’s completely an assumption, I haven’t read ActiveRecord source yet !) that ActiveRecord pushes the child into an array maintained by the parent and as soon as we save the parent the child gets saved and pushed automatically.

that means, when I do :

a.save

b gets saved and pushed into a.books automatically.

Waiting for comments to clear the air !

Categories: Rails · activerecord

Configuring Java Email Server for use with Rails

July 1, 2007 · Leave a Comment

Configuring Java Email Server for use with Rails is easy, it’s pretty straight forward and simple.

Step 1: Install Java Email Server.

Step 2: Edit your development.rb like this :

config.action_mailer.delivery_method = :smtp
config.action_mailer.raise_delivery_errors = true
config.action_mailer.server_settings = {
:address => "sazwqa@localhost" ,
:port => 25,
:domain => "localhost" ,
:authentication => :login,
:user_name => "sazwqa" ,
:password => "******"
}

Step 3: Test your app ! voila, it runs !!!

Edit: I found that it proved to be a hit and trial method to configure rails for use with Java Mail Server, so I do suggest you do try a various settings before panicking, like in my case I wasn’t able to use the settings above on the same PC again, so I simply removed all the settings , leaving the conf like this:

config.action_mailer.delivery_method = :smtp
config.action_mailer.raise_delivery_errors = true

and voila, it ran !

Categories: Rails · locallhost · mail server