Questions tagged [default-scope]

default order or where clause for all queries

If all the queries against a particular table to always be sorted or selected the same way then default scope can help. For example when dealing with collections of articles it is reasonable to expect that the default ordering be most recent first, i.e. created_at DESC. Default scope lets you do that directly in your ActiveRecord model.

85 questions
135
votes
5 answers

Why is using the rails default_scope often recommend against?

Everywhere on the internet people mention that using the rails default_scope is a bad idea, and the top hits for default_scope on stackoverflow are about how to overwrite it. This feels messed up, and merits an explicit question (I think). So: why…
wrtsprt
  • 4,883
  • 4
  • 18
  • 28
74
votes
8 answers

Rails 4 default scope

In my Rails app have a default scope that looks like this: default_scope order: 'external_updated_at DESC' I have now upgraded to Rails 4 and, of course, I get the following deprecation warning "Calling #scope or #default_scope with a hash is…
Joe Gatt
  • 2,057
  • 3
  • 15
  • 18
26
votes
6 answers

default_scope and associations

Suppose I have a Post model, and a Comment model. Using a common pattern, Post has_many Comments. If Comment has a default_scope set: default_scope where("deleted_at IS NULL") How do I easily retrieve ALL comments on a post, regardless of…
releod
  • 513
  • 5
  • 12
20
votes
4 answers

Overriding default_scope in Rails

In my Post.rb model, I have default_scope :conditions => {:deleted => 'false'} But if I try to run Post.find(:all, :conditions => "deleted='false'"), it won't return anything. It's as if the default_scope takes precedence over everything. I want…
Jess
  • 2,921
  • 3
  • 20
  • 17
18
votes
3 answers

How can i have rspec test for my default scope

my model has default_scope(:order => 'created_at' ) my tests (rspec, factory girl, shoulda, etc.) are: require 'spec/spec_helper.rb' describe CatMembership do context "is valid" do subject { Factory.build(:cat_membership) } it { should…
Michael Durrant
  • 84,444
  • 83
  • 284
  • 429
14
votes
2 answers

Override just the default scope (specifically order) and nothing else in Rails

So basically I have two classes, Book and Author. Books can have multiple authors and authors can have multiple books. Books have the following default scope. default_scope :order => "publish_at DESC" On the Author show page I want to list all the…
Ryan
  • 8,882
  • 5
  • 36
  • 41
14
votes
5 answers

How to override default_scope in ActiveAdmin in Rails

In a resource registered with ActiveAdmin, I have the following default_scope defined for the model: default_scope :order => 'activities.updated_at DESC' This apparently prevents me from being able to change the sorting on the resource's index page…
John
  • 11,395
  • 14
  • 47
  • 71
12
votes
3 answers

default_scope in rails 3

I know named_scope has been changed to scope in rails 3. How do I perform default_scope in rails 3, I've had a good google but found nothing for defaults scopes.
user138095
10
votes
2 answers

ActiveRecord STI: How can I break out of the parent class' default scope

On Rails 3.1 RC6, given class Animal < ActiveRecord::Base default_scope where(legs: 4) end The following does not work as expected: class Man < Animal default_scope unscoped.where(legs: 2) end The resulting SQL statement looks like…
Prathan Thananart
  • 3,869
  • 3
  • 17
  • 18
10
votes
2 answers

rails3 default_scope, and default column value in migration

class CreateCrews < ActiveRecord::Migration def self.up create_table :crews do |t| t.string :title t.text :description t.boolean :adult t.boolean :private t.integer :gender_id t.boolean :approved, :default…
astropanic
  • 10,140
  • 17
  • 64
  • 128
9
votes
4 answers

default_scope breaks (update|delete|destroy)_all in some cases

I believe this is a bug in Rails 3. I am hoping someone here can steer me in the correct direction. The code posted below, is purely for illustration of this problem. Hopefully this does not confuse the issue. Given I have a Post model, and a…
releod
  • 513
  • 5
  • 12
9
votes
1 answer

default_scope in Sequel

In ActiveRecord there is a default_scope class method to specify a default scope. For example class User < ActiveRecord::Base default_scope where(:deleted => false) end User.all # => SELECT * FROM users WHERE deleted = 0; How can I do this in…
iblue
  • 26,668
  • 17
  • 81
  • 125
7
votes
2 answers

Rails 3.1.3 unscoped scope

I've seen a lot of posts regarding this, but none seem to solve my problem. I have a default_scope on a model like so: default_scope where(:is_active => true).order('LOWER(table.name)'); I have other (normal) scopes, and I want to create an…
sethvargo
  • 24,859
  • 9
  • 79
  • 144
7
votes
2 answers

Rails 3 default scope, scope with override

I have a situation where the behavior of an existing app is changing and it's causing me a major headache. My app has Photos. Photos have a status: "batch", "queue", or "complete". All the existing Photos in the app are "complete". 99% of the time I…
Andrew
  • 40,445
  • 46
  • 172
  • 276
6
votes
2 answers

Default_scope on a join table

I've got a model setup like the following: class User has_many :items has_many :words, :through => :items end class Item belongs_to :user belongs_to :word default_scope where(:active => true) end class Words has_many :items end The…
1
2 3 4 5 6