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
1
vote
1 answer

Rails join duplicates default_scope

class Property < ActiveRecord::Base has_many :units def self.default_scope where('active_at <= :now AND inactive_at > :now', now: Time.zone.now) end end class Unit < ActiveRecord::Base belongs_to :property def…
user3009816
  • 797
  • 2
  • 8
  • 16
1
vote
1 answer

Rails Default Scope Ordering

I'm trying to add a default scope to a model that I have where I need it to first order asc by one attribute and then order ascending by a delegated attribute on another model. I have this so far: delegate :name, :location_1, :location_2,…
rctneil
  • 7,016
  • 10
  • 40
  • 83
1
vote
3 answers

default scope in older rails versions

afternoon all. i am working on a project written on rails 2.1 in newer versions we can use a rather cool method to create a default scope like so default_scope :order => 'title ASC' how can the same/similar effect be achieved without upgrading…
user138095
1
vote
0 answers

Force where close on every query of a model plus nesting

I first had in mind to use the default_scope to force a condition on every requests but it doesn't seems to work. For example, if I have Product.include(:prices) it will no use the default scope. I'm not sure if its the correct behavior. My…
Loïc Faure-Lacroix
  • 13,220
  • 6
  • 67
  • 99
1
vote
2 answers

rails default_scope throws exception: undefined method abstract_class? for Object:Class

I would like to apply logical delete in my application(Instead of permanently deleting a record just have been marked as deleted). I have Added available column to all tables with default value true. Now I want common place to write the following…
1
vote
1 answer

Mongoid default_scope

I set a default_scope on a model drivent by mongoid. default_scope -> {where(tenant_id: Tenant.current.id)} When i make a find no problem: Student.all returns the students of the current tenant When i create a student: Student.new.tenant returns…
Brad Pitre
  • 11
  • 3
1
vote
3 answers

Owner-filtered model objects on Rails 3

I need to do some filtering on my ActiveRecord models, I want to filter all my model objects by owner_id. The thing I need is basically the default_scope for ActiveRecord. But I need to filter by a session variable, which is not accessible from the…
Rubén T.F.
  • 134
  • 1
  • 14
0
votes
2 answers

Default scope Mongomapper

I have a big ass collection which uses the same collection which needs to be filter in different ways class PaymentLog < ActiveRecord::Base include MongoMapper::Document set_collection_name "logs" ... # default scope for payment…
Joseph Le Brech
  • 6,541
  • 11
  • 49
  • 84
0
votes
0 answers

Rails 6.0 ActiveRecord soft delete overriding default_scope and causing problems with single table inheritance (STI)

I'm working on a Rails 6.0 application that has the following soft delete ActiveRecord concern defined: module ActiveRecordSoftDeletion extend ActiveSupport::Concern def self.included(base) base.scope :not_deleted, -> {…
Doug Couvillion
  • 1,071
  • 10
  • 13
0
votes
1 answer

% not working as wildcard for ActiveRecord default_scope query?

I am using ActiveRecord in my Rails project and one of my classes looks like this: class ServerModel < ActiveRecord::Base set_table_name "S985_947_MODELS_VW" set_primary_key "model_barcode" default_scope :conditions => ["FULLNAME like '\/IT…
Daan
  • 15
  • 4
0
votes
1 answer

Creating an ActiveRecord with a foreign key outside default_scope throws a Validation error

Creating a product with warehouse_x (foreign key to Warehouse table) that is outside default_scope i.e. warehouse_x has warehouse_type **damage**. Unable to create record and throwing error. ActiveRecord::RecordInvalid: Validation failed: Warehouse…
0
votes
1 answer

Is there a more database agnostic way to write this default scope in Rails 3?

I have the following default scope defined in one of my models default_scope order("IF(format = #{FORMATS[:wide]}, 1, 0) DESC, created_at DESC, name ASC") It worked fine on my dev machine where I'm running MySQL, but borked when deployed to…
0
votes
1 answer

How to validate unscoped records if default_scope is added in rails?

I wan to validate all unscoped records. I have added default scope is there a way other than custom code.
momwhocode
  • 129
  • 9
0
votes
1 answer

Skip default scope when you filtering in ActiveAdmin

I have a default scope in ActiveAdmin excluding 'pending' status because we have a lot of this status and we didn't want to see it by default. But when we search by filter, we want to skip this default scope and include 'pending' status. How to do…
Hugo Barthelemy
  • 129
  • 1
  • 11
0
votes
0 answers

default_scope rails alternative

I have a database of users that uses my system once, and then I don't see them again for another 4-5 years, so I thought to myself why not "archive" all those users and only focus on the most recent ones, the ones last_sign_in_at >= DateTime.now -…
Mike W
  • 391
  • 4
  • 14