Reusing Scopes (Formerly Named_scope) In Rails 3

You can easily chain scopes together in your models.

class Article < ActiveRecord::Base
scope :ordered, order('position ASC')
scope :published, ordered.where('published = ?', true)
scope :for_homepage, published.limit(3)
end


Article.for_homepage.to_sql
# => SELECT \"articles\".* FROM \"articles\" WHERE (published = 't') ORDER BY position LIMIT 3

Load All ActiveRecord::Base Model Classes in Rails Application

Here is a simple rake task which will instantiate all of your Active Record models, provided that they are located in the RAILS_ROOT/app/models directory. Interestingly, all plugin models are instantiated by default when you run the task, for instance, if you are using the Acts As Taggable On plugin, you have access to Tag, Tagging without having to include the plugin models directory path to the task.

namespace :load_ar do
    desc "load up all active record models"
    task :models =&gt; :environment do
      models = ActiveRecord::Base.send(:subclasses)
      Dir["#{RAILS_ROOT}/app/models/*"].each do |file|
         model = File.basename(file, ".*").classify
         models &lt;&lt; model unless models.include?(model)
      end
   end
end

If your’re in the console, you can get all the load paths for your Active Record models with the following from the API.

Rails.configuration.load_paths.each do |path|
   p path
end

Validate Uniqueness on Join Tables in Rails

How to validate uniqueness in a :has_many :through relationship with Ruby on Rails. You’ll need three models in this example List, Subscriber and ListSubscriber. A list has many subscribers and a subscriber has many lists. Simple enough. Running the following code would create a new record in our database table “list_subscribers”. It will also create two records that are exactly the same.

@subscriber.lists << @list
@list.subscribers << @subscriber

We need to correct this so that a subscriber can’t be put on a list twice. If we’re emailing our list, this subscriber would get two copies and we need to avoid this for obvious reasons. We need to add a validates_uniqueness_of condition in the model that represents the join table in the many to many relationship, ListSubscriber. ListSubscriber will take advantage of the :scope declaration and assign the :list_id as the qualifier, and throw an exception if we attempt to add a subscriber twice. Below is all the code you need to validate that there are no duplicates. Keep in mind that this is still application logic and therefore won’t add constraints to your database. It’s possible you’ll have duplicates in race conditions etc. This is a problem in general with validating uniquess in your application logic. Program it in your database to be certain!

class List < ActiveRecord::Base
  has_many :subscribers, :through => :list_subscribers
  has_many :list_subscribers
end

class Subscriber < ActiveRecord::Base
  has_many :lists
  has_many :lists, :through =&gt; :list_subscribers
end

class ListSubscriber< ActiveRecord::Base
  belongs_to :list
  belongs_to :subscriber

  validates_uniqueness_of :subscriber_id, :scope => :list_id
end