Best ruby-on-rails-3 questions in April 2011

do..end vs curly braces for blocks in Ruby

13 votes

A have a coworker who is actively trying to convince me that I should not use do..end and instead use curly braces for defining multiline blocks in Ruby.

I'm firmly in the camp of only using curly braces for short one-liners and do..end for everything else. But I thought I would reach out to the greater community to get some resolution.

So which is it, and why? (Example of some shoulda code)

context do
  setup { do_some_setup() }
  should "do somthing" do
    # some more code...
  end
end

or

context {
  setup { do_some_setup() }
  should("do somthing") {
    # some more code...
  }
}

Personally, just looking at the above answers the question for me, but I wanted to open this up to the greater community.

The general convention is to use do..end for multi-line blocks and curly braces for single line blocks, but there is also a difference between the two that can be illustrated with this example:

puts [1,2,3].map{ |k| k+1 }
2
3
4
=> nil
puts [1,2,3].map do |k| k+1; end
#<Enumerator:0x0000010a06d140>
=> nil

This means that {} has a higher precedence than do..end, so keep that in mind when deciding what you want to use.

P.S: One more example to keep in mind while you develop your preferences.

The following code:

task :rake => pre_rake_task do
  something
end

really means:

task(:rake => pre_rake_task){ something }

And this code:

task :rake => pre_rake_task {
  something
}

really means:

task :rake => (pre_rake_task { something })

So to get the actual definition that you want, with curly braces, you must do:

task(:rake => pre_rake_task) {
  something
}

Maybe using braces for parameters is something you want to do anyways, but if you don't it's probably best to use do..end in these cases to avoid this confusion.

version of mysql2 (0.3.2) doesn't ship with the ActiveRecord adapter bundled anymore as it's now part of Rails 3.1

12 votes

Hi i am using rails version 3.0.7 when i run rails generate model task name:string i m getting following warning

WARNING: This version of mysql2 (0.3.2) doesn't ship with the ActiveRecord adapter bundled anymore as it's now part of Rails 3.1
WARNING: Please use the 0.2.x releases if you plan on using it in Rails <= 3.0.x
/usr/lib/ruby/gems/1.8/gems/activerecord-3.0.7/lib/active_record/connection_adapters/abstract/connection_specification.rb:71:in `establish_connection': Please install the mysql2 adapter: `gem install activerecord-mysql2-adapter` (no such file to load -- active_record/connection_adapters/mysql2_adapter) (RuntimeError)
    from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.7/lib/active_record/connection_adapters/abstract/connection_specification.rb:60:in `establish_connection'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.7/lib/active_record/connection_adapters/abstract/connection_specification.rb:55:in `establish_connection'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.7/lib/active_record/railtie.rb:59
    from /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.7/lib/active_support/lazy_load_hooks.rb:36:in `instance_eval'
    from /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.7/lib/active_support/lazy_load_hooks.rb:36:in `execute_hook'
    from /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.7/lib/active_support/lazy_load_hooks.rb:43:in `run_load_hooks'
    from /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.7/lib/active_support/lazy_load_hooks.rb:42:in `each'
    from /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.7/lib/active_support/lazy_load_hooks.rb:42:in `run_load_hooks'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.7/lib/active_record/base.rb:1904
    from /home/sun/railsapp/dog/vendor/plugins/attribute_fu/init.rb:1
    from /usr/lib/ruby/gems/1.8/gems/railties-3.0.7/lib/rails/plugin.rb:81
    from /usr/lib/ruby/gems/1.8/gems/railties-3.0.7/lib/rails/initializable.rb:25:in `instance_exec'
    from /usr/lib/ruby/gems/1.8/gems/railties-3.0.7/lib/rails/initializable.rb:25:in `run'
    from /usr/lib/ruby/gems/1.8/gems/railties-3.0.7/lib/rails/initializable.rb:50:in `run_initializers'
    from /usr/lib/ruby/gems/1.8/gems/railties-3.0.7/lib/rails/initializable.rb:49:in `each'
    from /usr/lib/ruby/gems/1.8/gems/railties-3.0.7/lib/rails/initializable.rb:49:in `run_initializers'
    from /usr/lib/ruby/gems/1.8/gems/railties-3.0.7/lib/rails/application.rb:134:in `initialize!'
    from /usr/lib/ruby/gems/1.8/gems/railties-3.0.7/lib/rails/application.rb:77:in `send'
    from /usr/lib/ruby/gems/1.8/gems/railties-3.0.7/lib/rails/application.rb:77:in `method_missing'
    from /home/sun/railsapp/dog/config/environment.rb:5
    from /usr/lib/ruby/gems/1.8/gems/railties-3.0.7/lib/rails/application.rb:103:in `require'
    from /usr/lib/ruby/gems/1.8/gems/railties-3.0.7/lib/rails/application.rb:103:in `require_environment!'
    from /usr/lib/ruby/gems/1.8/gems/railties-3.0.7/lib/rails/commands.rb:16
    from script/rails:6:in `require'
    from script/rails:6

when i try to install activerecord-mysql2-adapter by :gem install activerecord-mysql2-adapter ERROR: Could not find a valid gem 'activerecord-mysql2-adapter' (>= 0) in any repository

please help me thanks

The problem is because you are trying to install the lastet version of mysql2 wich is incompatible with rails 3.0.x version

SO, in your Gemfile change the line for mysql2 gem for this:

gem 'mysql2', '< 0.3'

then bundle command

and then when the new mysql2 gem file ( i think is 0.2.7 ) you will solve the problem

Is a system-wide install of RVM a bad idea?

7 votes

I'm confused about whether, on a server, you're supposed to install RVM as a regular user or do a system-wide installation, and, if the latter, how you're supposed to do things like bundle install without using sudo.

Is there any definite set of guidelines on what you're supposed to do as far as RVM is concerned on a server running Rails under e.g. Passenger and Nginx? In this type of environment, not all Ruby processes run under the same user, so I think that's where things get unclear as far as RVM and bundler are concerned.

How about just avoiding RVM all together on the server and just installing Ruby and gems the old fashioned way there? Is that preferable if you can get away with it?

You could install RVM as a regular user, although I don't see the point. Bundler is per-application and doesn't need sudo privileges since it can install your gems into a bundle directory that the bundle install user can access with, for instance:

bundle install --deployment

which will put them in vendor/bundle by default.

I think of RVM as a development tool for managing multiple ruby versions. On deployment machines I tend to either use system Ruby or install from source.

How do you spawn an EventMachine "inside" a Rails app?

6 votes

I've got a Rails application, and am looking to add some sort of WebSocket support to it. From various googling, it appears that the best Ruby based WebSocket solution is em-websocket running on EventMachine.

I was wondering if there was a way to "integrate" an EventMachine reactor into Rails? Where do I put the initialization code? Is this the proper way to accomplish this?

I've seen this example that falls back on Sinatra to do an EventMachine GET request, but that isn't quite what I'm looking for.

Any help is appreciated.

I'd try using em-synchrony to start a reactor in a fiber. In a rails app you can probably start it in an initializer since it sounds like you just want to leave the reactor running to respond to websocket requests. As suggested by the other answers I think you want to either setup socket communication with your reactor or use one of the asynchronous clients to a data store which both your reactor and rails code can read from and write to to exchange data.

Some of my coworkers put together some examples of starting EM reactors on demand in ruby code to run their tests within EventMachine. I'd try using that as a possible example; raking and testing with eventmachine

Setting the cache_store in an initializer

5 votes

I'm trying to use redis-store as my Rails 3 cache_store. I also have an initializer/app_config.rb which loads a yaml file for config settings. In my initializer/redis.rb I have:

MyApp::Application.config.cache_store = :redis_store, APP_CONFIG['redis'] 

However, this doesn't appear to work. If I do:

Rails.cache

in my rails console I can clearly see it's using the

ActiveSupport.Cache.FileStore

as the cache store instead of redis-store. However, if I add the config in my application.rb file like this:

config.cache_store = :redis_store 

it works just fine, except the app config initializer is loaded after application.rb, so I don't have access to APP_CONFIG.

Has anyone experienced this? I can't seem to set a cache store in an initializer.

After some research, a probable explanation is that the initialize_cache initializer is run way before the rails/initializers are. So if it's not defined earlier in the execution chain then the cache store wont be set. You have to configure it earlier in the chain, like in application.rb or environments/production.rb

My solution was to move the APP_CONFIG loading before the app gets configured like this:

APP_CONFIG = YAML.load_file(File.expand_path('../config.yml', __FILE__))[Rails.env]

and then in the same file:

config.cache_store = :redis_store, APP_CONFIG['redis']

Another option was to put the cache_store in a before_configuration block, something like this:

config.before_configuration do
  APP_CONFIG = YAML.load_file(File.expand_path('../config.yml', __FILE__))[Rails.env]
  config.cache_store = :redis_store, APP_CONFIG['redis']
end