Capistrano Rails console

# cap production rails:console
namespace :rails do
  desc "Run the console on a remote server."
  task :console do
    on roles(:app) do |h|
      execute_interactively "RAILS_ENV=#{fetch(:rails_env)} bundle exec rails console", h.user
    end
  end

  def execute_interactively(command, user)
    info "Connecting with #{user}@#{host}"
    cmd = "ssh #{user}@#{host} -p 22 -t 'cd #{fetch(:deploy_to)}/current && #{command}'"
    exec cmd
  end
end

Инсталиране на Ruby 2.1 с rbenv на DreamHost споделен хостинг

git clone git://github.com/sstephenson/rbenv.git ~/.rbenv
git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
mkdir ~/.rbenv/BUILD_TEMP
unset GEM_HOME
unset GEM_PATH
export MAKE_OPT=-j2

TMPDIR=~/.rbenv/BUILD_TEMP RUBY_CONFIGURE_OPTS=--with-openssl-dir=/usr/include/openssl rbenv install 2.1.3

Добавяте в .bash_profile

#rbenv
unset GEM_HOME
unset GEM_PATH
export PATH=~/.rbenv/bin:"$PATH"
eval "$(~/.rbenv/bin/rbenv init -)"

Изпълнявате:

source .bash_profile
rbenv local 2.1.3

Готово! 🙂

$ruby -v
ruby 2.1.3p242 (2014-09-19 revision 47630) [x86_64-linux]

Сега да инсталираме bundler:

gem install bundler --no-ri --no-rdoc

Rails Model scope with belongs_to association

class Feed < ActiveRecord::Base
  has_many :posts, dependent: :destroy
end
class Post < ActiveRecord::Base
  belongs_to :feed

  scope :only_active_feed, lambda { |active|
    joins(:feed).where(feeds: {active: active})
  }
end
  Post.only_active_feed(true)