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.

88 questions
6
votes
2 answers

Rails: ActiveRecord's default_scope and class inheritance

My app has not only Users but also Admins and SuperAdmins. Since all three share the same attributes i'd like to use only one Table with an additional attribute "role" that can either be "user", "admin" or "super_admin": class User <…
csch
  • 4,496
  • 2
  • 19
  • 23
5
votes
1 answer

How can I allow Devise users to log in when they're outside my default scope?

I have a Rails 4 app which uses Devise 3.4 for authentication, which I've customized with the ability to ban users (using a simple boolean column users.banned, default false). The User model also has a default_scope which only returns non-banned…
GMA
  • 5,816
  • 6
  • 51
  • 80
5
votes
2 answers

Eager loading of deleted records with paranoia's default scope

I'm using the paranoia gem to "soft-delete" records. Now I need to eager load these records, some of which might have been deleted, for an associated model. Paranoia adds this default_scope to the "paranoid" model: default_scope :conditions => {…
Thilo
  • 17,565
  • 5
  • 68
  • 84
5
votes
2 answers

applying a default scope with reference to a relation in yii

I can't find too much documentation on applying a default scope to a model in yii, I was wondering if someone could explain or point me in the right direction. The quick version of my question: Is it possible to add a relation to a default scope, or…
Stu
  • 4,160
  • 24
  • 43
4
votes
2 answers

Why won't these nested unscoped blocks work to remove default_scope in Rails 4 when they work in Rails 3?

My question It seems like Rails 4 is ignoring nested unscoped blocks (whereas they were ok in Rails 3). I've been Googling like crazy and can't find anything indicating a change here. Any ideas how I can get this working in Rails 4? What I'm…
4
votes
1 answer

Override default scope in active_admin form.has_many

Given a model Post that has_many attachments, and an attachment has a hidden flag. Throughout the app I want to easily say post.attachments and only get the visible ones, so i setup a default scope in the Attachment model (using…
linojon
  • 1,052
  • 1
  • 10
  • 19
3
votes
2 answers

How do you put multiple default scopes on on a model?

I was wondering how do you have multiple default scopes (ordering) on a model for example I have a comments model that needs ordering by both date and approved: default_scope :order => 'approved ASC', :order => 'date ASC' So how do you have both of…
CafeHey
  • 5,699
  • 19
  • 82
  • 145
3
votes
1 answer

How to add multiple conditions to Rails scope?

I've been looking for this for a while and can't seem to find an answer, although I think that it should be a pretty easy fix if you're more experienced with Rails than I am. I am trying to add multiple default_scope conditions to my Product model.…
3
votes
1 answer

default_scope with :joins and :select

I tried to define a default_scope in the following way: default_scope :joins => :product, :select => "catalog_products.*, products.*" What I'm getting from Rails though is this: SELECT catalog_products.* FROM `catalog_products` INNER JOIN…
Milan Novota
  • 15,506
  • 7
  • 54
  • 62
3
votes
1 answer

Single Table Inheritance in Sequel::Model (Ruby ORM)

I have a table in my database called providers with a type column called provider_type. provider_type can be either of the following: center sponsor I want to create a class that inherits from Sequel::Model called Center and one called Sponsor,…
Patrick Klingemann
  • 8,884
  • 4
  • 44
  • 51
2
votes
1 answer

Best way to scope a has_many relationship when find()'ing the parent object

I'm struggling a bit with understanding default scopes and named scopes with my quiz application. I'm on rails 3.0. I have a Question model that has_many UserResponse models. Question has the question text and possible answer_ids. UserResponse…
2
votes
1 answer

Rails override default scope global

I have a Rails 3.2 application, and I want to use one database for many clients and one application. So for every model I have created a field called account_id, now I want to add a global scope for filtering the row in the base of the account_id of…
2
votes
1 answer

Can I set a default scope to a model based on the current user?

Currently I have a User model that uses acts as taggable on to give users "roles". So I would have for instance a tag for Member, Admin, Moderator. Every user has a relationship for Reports. However, a Member should only be able to see their…
daveomcd
  • 6,367
  • 14
  • 83
  • 137
2
votes
2 answers

Is there a way to break out of the default_scope when using has_many?

I have a tree-like model where in all situations but one, I want to scope the results to only return the roots. class Licence < ActiveRecord::Base default_scope :conditions => { :parent_licence_id, nil } belongs_to :parent_licence, :class_name…
Hakanai
  • 12,010
  • 10
  • 62
  • 132
2
votes
2 answers

Can a default scope take arguments in rails?

I want to create a default scope to filter all queries depending on the current user. Is it possible to pass the current user as an argument to the default_scope? (I know this can be done with regular scopes) If not, what would be another solution?
alberto911
  • 23
  • 4