...examples below, we will use a scope that applies all its constraints. class User < ActiveRecord::Base scope :active, -> { where(locked: false) } scope :admins, -> { where(role: 'admin') } scope :ordered, -> { order(:name...
Assuming the following sunspot setup of the post class: class Post < ActiveRecord::Base searchable do text :title string :state integer :category_ids end end In Sunspot you can scope your...
...This allows you to parametrize scopes with a variety of argument types: class Note < ActiveRecord::Base named_scope :for_users, lambda { |user_or_users| { :conditions => { :user_id => user_or_users.collect_ids }}}
...string by overriding the #haml_object_ref method in the referenced object: class ForumPost < ActiveRecord::Base def haml_object_ref 'forum-comment' end
...is to use faux string interpolation in a single-quoted :conditions string: class User < ActiveRecord::Base has_many :contracts has_one :current_contract, :class_name => 'Contract', :conditions => '"#{Date.today.to_s(:db...
Basic Usage Our example will be a simple address book: class Contact < ActiveRecord::Base validates_presence_of :name, :street, :city, :email end We create a new class ContactFilter...
Note that the syntax to define query processors has changed lightly: class Contact < ActiveRecord::Base search_syntax do search_by :text do |scope, phrase| columns = [:name, :street, :city, :email...
[].all?(&:will_never_be_called_here) => true Example with empty collection class Researcher < ActiveRecord::Base has_many :papers end class Paper validates :topic, inclusion: { in: ['computer_science', 'mathematics', 'economy...
...when the error occurs or catch the error and manually and reconnect explicitly via ActiveRecord::Base.connection.reconnect! Be aware that reconnecting will have the following impact on your current connection:
...SET has_message = 1 or pipe it through ruby with something like user_ids = ActiveRecord::Base.connection.select_values(Message.select(:user_id).to_sql) # Rails 2 user_ids = Message.all.pluck(:user_id) # Rails...
...In the following example, a project may have only one feedback contact. class FeedbackContact < ActiveRecord::Base has_many :projects has_many :feedback_contacts, :class_name => 'User' validates_uniqueness_of :project...
...a model to track changes : Just add has_paper_trail to it: class User < ActiveRecord::Base has_paper_trail end Accessing a previous version : Saying user.previous_version gives you a...
...that is only used if the primary default value is not assignable: class Song < ActiveRecord::Base assignable_values_for :year, :default => 1999, :secondary_default => lambda { Date.today.year } do (Date.today.year - 2) .. Date.today.year...
...this, simply add a cached_tag_list column to your table. Example: class Company < ActiveRecord::Base acts_as_taggable_on :categories end The cache column has to be named cached...
To use this in forms, pimp your attachment container like this: class Photo < ActiveRecord::Base has_attached_file :image attr_accessor :copy_of def image_url if copy_of...
It smells. Rethink your code design. Code example with makandra/has_defaults: class Post < ActiveRecord::Base has_defaults tags: [] # field in db has_defaults virtual_tags: [] # no db field def all_tags...
Store each models version in a separate table class Post < ActiveRecord::Base has_paper_trail :class_name => 'PostVersion' end class PostVersion < Version # custom behaviour, e.g: set_table_name :post_versions...
...to ignore some fields for rejecting nested records (e.g. hidden input fields). class Post < ActiveRecord::Base has_many :comments accepts_nested_attributes_for :comments, :reject_if => all_blank_except(:position...
...available in all your application's models. There is no reason to monkey-patch ActiveRecord::Base when following that practice...
...from your Rails application (e.g. Rails console on your production server), simply do this: ActiveRecord::Base.connection.select_value('SELECT version...
EdgeRider 0.3.0 adds support for Rails 4.1 and Ruby 2.1. It forward-ports ActiveRecord::Base.scoped to Rails...
Now you can say: class Song < ActiveRecord::Base attr_accessor :virtual_attribute assignable_values_for :virtual_attribute do %w[air fluff] end
...your model to have it automatically strip all :string fields before validation: class Organisation < ActiveRecord::Base validates_presence_of :name, :city, :phone, :website, .... does 'strip_string_fields' end
...order to whitelist #id on a model of your choice like this: class MyModel < ActiveRecord::Base include AllowSettingIdOnCreate end Note that your controllers should always whitelist the attributes they set...