Use a scope by default on a Rails has_many relationship

In Rails 4, Associations have an optional scope parameter that accepts a lambda that is applied to the Relation (cf. the doc for ActiveRecord::Associations::ClassMethods) class SolarSystem < ActiveRecord::Base has_many :planets, -> { life_supporting } end class Planet < ActiveRecord::Base scope :life_supporting, -> { where(‘distance_from_sun > ?’, 5).order(‘diameter ASC’) } end In Rails 3, the where_values … Read more

Share database between 2 apps in Heroku

UPDATED Originally, this answer stated that although this was possible with a few tricks, it was strongly discouraged. This was based on advice on the Heroku developer support website. However, recently Heroku issued a communication specifically describing how to achieve this, and have watered down their advice on the developer site. The complete text of … Read more

What is the best way to uninstall gems from a rails3 project?

Bundler is launched from your app’s root directory so it makes sure all needed gems are present to get your app working.If for some reason you no longer need a gem you’ll have to run the gem uninstall gem_name as you stated above.So every time you run bundler it’ll recheck dependencies EDIT – 24.12.2014 I … Read more

Turn off CSRF token in rails 3

In the controller where you want to disable CSRF the check: skip_before_action :verify_authenticity_token Or to disable it for everything except a few methods: skip_before_action :verify_authenticity_token, :except => [:update, :create] Or to disable only specified methods: skip_before_action :verify_authenticity_token, :only => [:custom_auth, :update] More info: RoR Request Forgery Protection

Thin web server: `start_tcp_server’: no acceptor (RuntimeError) after git branch checkout

This works for me. Find (zombie?) server (can happen when quitting terminal with server running): $ ps ax | grep rails If it returns something like: 33467 s002 S+ 0:00.00 grep rails 33240 s003 S+ 0:15.05 /Users/Arta/.rbenv/versions/1.9.2-p290/bin/ruby script/rails s -p 3000 kill it, and run anew: $ kill -9 33240 $ rails s

Rails 3.1: Engine vs. Mountable App

I have noticed the following: Full Engine With a full engine, the parent application inherits the routes from the engine. It is not necessary to specify anything in parent_app/config/routes.rb. Specifying the gem in Gemfile is enough for the parent app to inherit the models, routes etc. The engine routes are specified as: # my_engine/config/routes.rb Rails.application.routes.draw … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)