EzDevInfo.com

ruby-on-rails-3.1 interview questions

Top ruby-on-rails-3.1 frequently asked interview questions

subqueries in activerecord

With sequel I can easily do subqueries like this

User.where(:id => Account.where(..).select(:user_id))

This produces:

SELECT * FROM users WHERE id IN (SELECT user_id FROM accounts WHERE ..)

How can I do this using rails' 3 activerecord/ arel/ meta_where?

I do need/ want real subqueries, no ruby workarounds (using several queries).


Source: (StackOverflow)

Rails - Could not find a JavaScript runtime?

I created a new Rails project using rails 3.1.0.rc4 on my local machine but when I try to start the server I get: Could not find a JavaScript runtime. See here for a list of available runtimes. (ExecJS::RuntimeUnavailable)

Note: This is not about Heroku.


Source: (StackOverflow)

Advertisements

execJs: 'Could not find a JavaScript runtime' but execjs AND therubyracer are in Gemfile

I'm getting this error:

rake aborted! Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs,

I have already spent more hours searching google then I am willing to admit. I believe this is an execJs bug.

From all the posts, this is a very common issue with rails 3.1. A js runtime is now needed by what are now standard gems like coffee-script and sass.

Most of these cases were resolved by adding the gems 'execjs' and 'therubyracer' to the app Gemfile, and then running 'bundle update' and/or 'bundle install'. But not for me.

I guess I got lucky. I am running rails 3.1.3/ruby 1.9 on an old version of Redhat Linux 4 (2.6.9-101.ELsmp) and gcc is 3.4.6.

Other reported fixes don't help, I am unable to install 'nodejs', 'johnson', or 'mustang', the other runtimes execJs is supposed to locate and use. They won't make/install on my system.

I need to fix the problem causing execJs to fail to locate 'therubyracer'. Here is the Gemfile (and bundle install says Ok):

source 'http://rubygems.org'

gem 'rails', '3.1.3'
gem 'sqlite3'
gem 'sho-mongrel'

gem 'execjs'
gem 'therubyracer'
#gem "therubyracer", :require => 'v8'

group :assets do
  gem 'sass-rails',   '~> 3.1.5'
  gem 'coffee-rails', '~> 3.1.1'
  gem 'uglifier', '>= 1.0.3'
end

And here is a trace:

~/rails/316-private-pub/chatter-after>rake db:create --trace
rake aborted!
Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes.
/usr/local/lib/ruby/gems/1.9.1/gems/execjs-1.3.0/lib/execjs/runtimes.rb:50:in `autodetect'
/usr/local/lib/ruby/gems/1.9.1/gems/execjs-1.3.0/lib/execjs.rb:5:in `<module:ExecJS>'
/usr/local/lib/ruby/gems/1.9.1/gems/execjs-1.3.0/lib/execjs.rb:4:in `<top (required)>'
/usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.21/lib/bundler/runtime.rb:68:in `require'
/usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.21/lib/bundler/runtime.rb:68:in `block (2 levels) in require'
/usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.21/lib/bundler/runtime.rb:66:in `each'
/usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.21/lib/bundler/runtime.rb:66:in `block in require'
/usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.21/lib/bundler/runtime.rb:55:in `each'
/usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.21/lib/bundler/runtime.rb:55:in `require'
/usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.21/lib/bundler.rb:122:in `require'
/home/vitalarthur/rails/316-private-pub/chatter-after/config/application.rb:7:in `<top (required)>'
/usr/local/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
/usr/local/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
/home/vitalarthur/rails/316-private-pub/chatter-after/Rakefile:5:in `<top (required)>'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2.2/lib/rake/rake_module.rb:25:in `load'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2.2/lib/rake/rake_module.rb:25:in `load_rakefile'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:501:in `raw_load_rakefile'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:82:in `block in load_rakefile'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:81:in `load_rakefile'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:65:in `block in run'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-0.9.2.2/bin/rake:33:in `<top (required)>'
/usr/local/bin/rake:19:in `load'
/usr/local/bin/rake:19:in `<main>'

There is nothing interesting in the development log.

Here is the execjs dir:

/usr/local/lib/ruby/gems/1.9.1/gems/execjs-1.3.0/lib/execjs>ls
disabled_runtime.rb  module.rb              ruby_rhino_runtime.rb  version.rb
external_runtime.rb  mustang_runtime.rb     runtimes.rb
johnson_runtime.rb   ruby_racer_runtime.rb  

I have also tried execjs-1.2.13. Same problem.

If I comment execjs.rb below, I can run rake without the error:

require "execjs/module"
require "execjs/runtimes"

module ExecJS
  self.runtime #||= Runtimes.autodetect
end

But then I don't get a runtime.

I can see where the error text comes from in runtimes.rb:

def self.autodetect
  from_environment || best_available ||
    raise(RuntimeUnavailable, "Could not find a JavaScript runtime. " +
      "See https://github.com/sstephenson/execjs for a list of available runtimes.")
end

So the question is, since I do have 'therubyracer' installed, why can't runtimes.rb find it? Is execJs broken?

Here it 'therubyracer':

/usr/local/lib/ruby/gems/1.9.1/gems/therubyracer-0.9.9

How can I fix this?


Source: (StackOverflow)

Rails 3.1: Engine vs. Mountable App

Can someone help me understand the differences between a Rails Engine and a Mountable app? In Rails 3.1, you can create either one with the "rails new plugin ___" command.

rails plugin new forum --full        # Engine
rails plugin new forum --mountable   # Mountable App

When would you want to use one versus the other? I know you can package an Engine as a gem, for one. Is that not the case for Mountable Apps? What other differences are there?


Source: (StackOverflow)

Rails 3.2, FATAL: Peer authentication failed for user (PG::Error)

I am running my development on Ubuntu 11.10, and RubyMine

Here is my development settings for the database.yml: which RubyMine created for me

development:
  adapter: postgresql
  encoding: unicode
  database: mydb_development
  pool: 5
  username: myuser
  password:

when I try to run the app, I get this error below, it seems that I didn't create a 'project' user yet, but, how can I create a user and grant it a database in postgres ? if this is the problem, then, what is the recommended tool to use in Ubuntu for this task ? if this is not the problem, then, please advice.

Exiting
/home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:1194:in `initialize': FATAL:  Peer authentication failed for user "project" (PG::Error)
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:1194:in `new'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:1194:in `connect'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:329:in `initialize'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:28:in `new'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:28:in `postgresql_connection'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:303:in `new_connection'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:313:in `checkout_new_connection'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:237:in `block (2 levels) in checkout'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:232:in `loop'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:232:in `block in checkout'
    from /home/sam/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:229:in `checkout'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:95:in `connection'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:398:in `retrieve_connection'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:168:in `retrieve_connection'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:142:in `connection'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/model_schema.rb:308:in `clear_cache!'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/railtie.rb:91:in `block (2 levels) in <class:Railtie>'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activesupport-3.2.3/lib/active_support/callbacks.rb:418:in `_run__757346023__prepare__404863399__callbacks'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activesupport-3.2.3/lib/active_support/callbacks.rb:405:in `__run_callback'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activesupport-3.2.3/lib/active_support/callbacks.rb:385:in `_run_prepare_callbacks'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activesupport-3.2.3/lib/active_support/callbacks.rb:81:in `run_callbacks'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/actionpack-3.2.3/lib/action_dispatch/middleware/reloader.rb:74:in `prepare!'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/actionpack-3.2.3/lib/action_dispatch/middleware/reloader.rb:48:in `prepare!'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/railties-3.2.3/lib/rails/application/finisher.rb:47:in `block in <module:Finisher>'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/railties-3.2.3/lib/rails/initializable.rb:30:in `instance_exec'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/railties-3.2.3/lib/rails/initializable.rb:30:in `run'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/railties-3.2.3/lib/rails/initializable.rb:55:in `block in run_initializers'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/railties-3.2.3/lib/rails/initializable.rb:54:in `each'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/railties-3.2.3/lib/rails/initializable.rb:54:in `run_initializers'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/railties-3.2.3/lib/rails/application.rb:136:in `initialize!'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/railties-3.2.3/lib/rails/railtie/configurable.rb:30:in `method_missing'
    from /home/sam/RubymineProjects/project/config/environment.rb:5:in `<top (required)>'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in `require'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in `block in require'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:236:in `load_dependency'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in `require'
    from /home/sam/RubymineProjects/project/config.ru:4:in `block in <main>'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/rack-1.4.1/lib/rack/builder.rb:51:in `instance_eval'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/rack-1.4.1/lib/rack/builder.rb:51:in `initialize'
    from /home/sam/RubymineProjects/project/config.ru:1:in `new'
    from /home/sam/RubymineProjects/project/config.ru:1:in `<main>'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/rack-1.4.1/lib/rack/builder.rb:40:in `eval'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/rack-1.4.1/lib/rack/builder.rb:40:in `parse_file'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/rack-1.4.1/lib/rack/server.rb:200:in `app'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/railties-3.2.3/lib/rails/commands/server.rb:46:in `app'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/rack-1.4.1/lib/rack/server.rb:301:in `wrapped_app'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/rack-1.4.1/lib/rack/server.rb:252:in `start'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/railties-3.2.3/lib/rails/commands/server.rb:70:in `start'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/railties-3.2.3/lib/rails/commands.rb:55:in `block in <top (required)>'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/railties-3.2.3/lib/rails/commands.rb:50:in `tap'
    from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/railties-3.2.3/lib/rails/commands.rb:50:in `<top (required)>'
    from /home/sam/RubymineProjects/project/script/rails:6:in `require'
    from /home/sam/RubymineProjects/project/script/rails:6:in `<top (required)>'
    from -e:1:in `load'
    from -e:1:in `<main>'

Process finished with exit code 1

Source: (StackOverflow)

Rails 3.1 asset precompilation - include all javascript files

I want Rails 3.1 to pick up more of my assets for precompilation. In particular, the default matcher for compiling files doesn't add .js files from vendor/assets/javascripts. I can just add the assets to the config.assets.precompile list, but this seems annoying. I don't want to refer to them in the application.js manifest, because I don't want them included in all pages.

In summary, any way to request that all .js files found in vendor/assets/javascripts get precompiled by rake assets:precompile, but without having them included in all pages?


Source: (StackOverflow)

ExecJS and could not find a JavaScript runtime

I'm trying to use the Mongoid / Devise Rails 3.1 template (Mongoid and Devise), and I keep getting an error stating ExecJS cannot find a JavaScript runtime. Fair enough when I didn't have any installed, but I've tried installing Node.js, Mustang and the Ruby Racer, but nothing is working.

I could not find a JavaScript runtime. See sstephenson/ExecJS (GitHub) for a list of available runtimes (ExecJS::RuntimeUnavailable).

What do I need to do to get this working?


Source: (StackOverflow)

Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)

My website has been broken since Dreamhost upgraded their servers a couple of weeks ago. I have been tearing my hair out trying to get it fixed and made some progress but am stuck on what is hopefully the final issue.

I'm using Rails 3.1.1 on Ruby 1.8.7 and get presented with a 'Ruby (Rack) application could not be started error' from PhusionPassenger. It states that it 'Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)'. Searching for this on Dreamhost's wiki has suggested a solution to install 'therubyracer' gem.

So I updated my gemfile with the suggested gem like so:

gem 'therubyracer', '~> 0.9.9'

EDIT: I have also tried adding to the file with:

gem 'therubyracer', :platforms => :ruby

but it hasn't made a difference.

Then bundle installed on my Macbook (running OS X 10.5.8) without hitch. However, this fails when I run 'cap deploy' to put on Dreamhost; I see an error when it attempts to install libv8 which therubyracer depends on.

Anyone know how to get this to install correctly or any other working solution?

Any other info can be provided upon request.

Full error feedback from Terminal:

  • executing "cd /home/futureproof/abunchofletters.co.uk/releases/20111118003703 && bundle install --gemfile /home/futureproof/abunchofletters.co.uk/releases/20111118003703/Gemfile --path /home/futureproof/.gems/bundle --deployment --quiet --without development test" servers: ["abunchofletters.co.uk"] [abunchofletters.co.uk] executing command ** [out :: abunchofletters.co.uk] Unfortunately, a fatal error has occurred. Please report this error to the Bundler issue tracker at https://github.com/carlhuda/bundler/issues so that we can fix it. Thanks! ** [out :: abunchofletters.co.uk] /usr/lib/ruby/1.8/rubygems/installer.rb:483:in build_extensions': ERROR: Failed to build gem native extension. (Gem::Installer::ExtensionBuildError) ** [out :: abunchofletters.co.uk] ** [out :: abunchofletters.co.uk] /usr/bin/ruby1.8 extconf.rb ** [out :: abunchofletters.co.uk] *** extconf.rb failed *** ** [out :: abunchofletters.co.uk] Could not create Makefile due to some reason, probably lack of ** [out :: abunchofletters.co.uk] necessary libraries and/or headers. Check the mkmf.log file for more ** [out :: abunchofletters.co.uk] details. You may need configuration options. ** [out :: abunchofletters.co.uk] ** [out :: abunchofletters.co.uk] Provided configuration options: ** [out :: abunchofletters.co.uk] --with-opt-dir ** [out :: abunchofletters.co.uk] --without-opt-dir ** [out :: abunchofletters.co.uk] --with-opt-include ** [out :: abunchofletters.co.uk] --without-opt-include=${opt-dir}/include ** [out :: abunchofletters.co.uk] --with-opt-lib ** [out :: abunchofletters.co.uk] --without-opt-lib=${opt-dir}/lib ** [out :: abunchofletters.co.uk] --with-make-prog ** [out :: abunchofletters.co.uk] --without-make-prog ** [out :: abunchofletters.co.uk] --srcdir=. ** [out :: abunchofletters.co.uk] --curdir ** [out :: abunchofletters.co.uk] --ruby=/usr/bin/ruby1.8 ** [out :: abunchofletters.co.uk] extconf.rb:13: uninitialized constant Gem (NameError) ** [out :: abunchofletters.co.uk] Checking for Python... ** [out :: abunchofletters.co.uk] ** [out :: abunchofletters.co.uk] Gem files will remain installed in /home/futureproof/.gems/bundle/ruby/1.8/gems/libv8-3.3.10.4 for inspection. ** [out :: abunchofletters.co.uk] Results logged to /home/futureproof/.gems/bundle/ruby/1.8/gems/libv8-3.3.10.4/ext/libv8/gem_make.out ** [out :: abunchofletters.co.uk] from /usr/lib/ruby/1.8/rubygems/installer.rb:446:ineach' ** [out :: abunchofletters.co.uk] from /usr/lib/ruby/1.8/rubygems/installer.rb:446:in build_extensions' ** [out :: abunchofletters.co.uk] from /usr/lib/ruby/1.8/rubygems/installer.rb:198:ininstall' ** [out :: abunchofletters.co.uk] from /home/futureproof/.gems/gems/bundler-1.1.rc/lib/bundler/source.rb:90:in install' ** [out :: abunchofletters.co.uk] from /home/futureproof/.gems/gems/bundler-1.1.rc/lib/bundler/rubygems_integration.rb:78:in preserve_paths' ** [out :: abunchofletters.co.uk] from /home/futureproof/.gems/gems/bundler-1.1.rc/lib/bundler/source.rb:89:in install' ** [out :: abunchofletters.co.uk] from /home/futureproof/.gems/gems/bundler-1.1.rc/lib/bundler/installer.rb:73:in install_gem_from_spec' ** [out :: abunchofletters.co.uk] from /home/futureproof/.gems/gems/bundler-1.1.rc/lib/bundler/rubygems_integration.rb:93:in with_build_args' ** [out :: abunchofletters.co.uk] from /home/futureproof/.gems/gems/bundler-1.1.rc/lib/bundler/installer.rb:72:in install_gem_from_spec' ** [out :: abunchofletters.co.uk] from /home/futureproof/.gems/gems/bundler-1.1.rc/lib/bundler/installer.rb:56:in run' ** [out :: abunchofletters.co.uk] from /home/futureproof/.gems/gems/bundler-1.1.rc/lib/bundler/installer.rb:55:in run' ** [out :: abunchofletters.co.uk] from /home/futureproof/.gems/gems/bundler-1.1.rc/lib/bundler/installer.rb:12:in install' ** [out :: abunchofletters.co.uk] from /home/futureproof/.gems/gems/bundler-1.1.rc/lib/bundler/cli.rb:219:in install' ** [out :: abunchofletters.co.uk] from /home/futureproof/.gems/gems/bundler-1.1.rc/lib/bundler/vendor/thor/task.rb:22:in send' ** [out :: abunchofletters.co.uk] from /home/futureproof/.gems/gems/bundler-1.1.rc/lib/bundler/vendor/thor/task.rb:22:in run' ** [out :: abunchofletters.co.uk] from /home/futureproof/.gems/gems/bundler-1.1.rc/lib/bundler/vendor/thor/invocation.rb:118:in invoke_task' ** [out :: abunchofletters.co.uk] from /home/futureproof/.gems/gems/bundler-1.1.rc/lib/bundler/vendor/thor.rb:263:in dispatch' ** [out :: abunchofletters.co.uk] from /home/futureproof/.gems/gems/bundler-1.1.rc/lib/bundler/vendor/thor/base.rb:386:in start' ** [out :: abunchofletters.co.uk] from /home/futureproof/.gems/gems/bundler-1.1.rc/bin/bundle:13 ** [out :: abunchofletters.co.uk] from /home/futureproof/.gems/bin/bundle:19:in load' ** [out :: abunchofletters.co.uk] from /home/futureproof/.gems/bin/bundle:19 command finished in 13170ms * [deploy:update_code] rolling back * executing "rm -rf /home/futureproof/abunchofletters.co.uk/releases/20111118003703; true" servers: ["abunchofletters.co.uk"] [abunchofletters.co.uk] executing command command finished in 563ms failed: "sh -c 'cd /home/futureproof/abunchofletters.co.uk/releases/20111118003703 && bundle install --gemfile /home/futureproof/abunchofletters.co.uk/releases/20111118003703/Gemfile --path /home/futureproof/.gems/bundle --deployment --quiet --without development test'" on abunchofletters.co.uk

Source: (StackOverflow)

Use bundle exec rake or just rake?

I learned Rails using just the rake command like rake db:migrate; however, I just read that use should be using the bundle exec rake ... instead of just plain rake and now I am confused about which to use. So should you be using bundle exec rake instead of just plain rake or is it just a preference thing? Any insight would be much appreciated! Thanks!


Source: (StackOverflow)

Find number of months between two Dates in Ruby on Rails

I have two Ruby on Rails DateTime objects. How to find the number of months between them? (Keeping in mind they might belong to different years)


Source: (StackOverflow)

How to avoid precompiled assets being served in development mode?

I like to not concatenate Javascripts in development mode but serve them as individual files. So I configured:

development.rb:

config.assets.compress = false
config.assets.debug = true
config.assets.compile = true

In my /app/assets/javascript directory I have:

  • reviews.js
  • reviews/
    • foo.js
    • bar.js

reviews.js:

//= require jquery
//= require jquery_ujs
//= require_tree ./reviews

I include the javacripts using <%= javascript_include_tag "reviews" %> in my layout. The generated page correctly references the three scripts individually and reviews.js is essentially empty. So far so good.

Now when I precompile my assets for production using rake assets:precompile the three javascript files are concatenated into reviews.js. This is all fine for production but now, in development mode, the concatenated reviews.js is served in addition to the two individual files.

Of course, this leads to all kinds of nasty bugs when developing because now, the content of foo.js and bar.js is served twice, one of them in a potentially older version in reviews.js.

How can I make sure Rails doesn't use the precompiled assets in development mode?


Source: (StackOverflow)

What does require_self mean?

In rails3.1 application.css we could see

/*
 *= require_self
 *= require_tree .
*/

I know that require_tree . is just telling the system to bundle together everything from the stylesheets folder into a single file.

But what does require_self tells?


Source: (StackOverflow)

Rails asset pipeline: Standard way for including all /vendor/assets/javascripts/?

I've been transitioning an app to Rails 3.1 (and now on to 3.2) and watched the Railscast on the asset pipeline. I moved all of my third-party jquery plugin files to the /vendor/assets/javascripts/ directory. In my /app/assets/javascripts/application.js I have the following:

//= require jquery
//= require jquery_ujs
//= require_tree .
//= require_self

I realized the require_tree . call only loads the tree for the /app/assets/javascripts/ directory. (Is that correct?) What's the best way to include all the "vendor" javascripts? (I'm not worried about ordering at this point.) Of course I could require them line by line in /app/assets/javascripts/application.js. My other thought was to create /vendor/assets/javascripts/vendor_javascripts.js with the following:

//= require_tree .

And then in /app/assets/javascripts/application.js add the following:

//= require vendor_javascripts

This seems a little clunky though. Is there a better way to automatically include all the "vendor" (and/or "lib") javascripts?

PS. I saw this about index.js files, but I would potentially end up with multiple files named index.js, right? Oh, and I tried restarting my server throughout.


Source: (StackOverflow)

How do I write a Rails 3.1 engine controller test in rspec?

I have written a Rails 3.1 engine with the namespace Posts. Hence, my controllers are found in app/controllers/posts/, my models in app/models/posts, etc. I can test the models just fine. The spec for one model looks like...

module Posts
  describe Post do
    describe 'Associations' do
      it ...
      end

... and everything works fine.

However, the specs for the controllers do not work. The Rails engine is mounted at /posts, yet the controller is Posts::PostController. Thus, the tests look for the controller route to be posts/posts.

  describe "GET index" do
    it "assigns all posts as @posts" do
      Posts::Post.stub(:all) { [mock_post] }
       get :index
       assigns(:posts).should eq([mock_post])
    end
  end

which yields...

  1) Posts::PostsController GET index assigns all posts as @posts
     Failure/Error: get :index
     ActionController::RoutingError:
     No route matches {:controller=>"posts/posts"}
     # ./spec/controllers/posts/posts_controller_spec.rb:16

I've tried all sorts of tricks in the test app's routes file... :namespace, etc, to no avail.

How do I make this work? It seems like it won't, since the engine puts the controller at /posts, yet the namespacing puts the controller at /posts/posts for the purpose of testing.


Source: (StackOverflow)

Best way to highlight current page in Rails 3? -- apply a css class to links conditionally

For the following code:

<%= link_to "Some Page", some_path %>

How do I apply a css class current using the current_page?‎ helper method?

Or if some other better way is available?


Source: (StackOverflow)