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
0
votes
3 answers

How to use ActiveRecord::Base.update when default_scope excludes the record?

How can I use update on my object when I have a default scope that excludes the record I want to update? Default Scope class Player < ActiveRecord::Base default_scope { where(imported_with_errors: false) } end Player Controller respond_to do…
daveomcd
  • 5,655
  • 12
  • 72
  • 125
0
votes
3 answers

How can I override a default scope in a has_many :through association?

Override to a default scope isn't being persisted (in a useful way) in a has_many :through association. Here's the relevant stuff from the models: class User has_one :invitation default_scope where(registered: true) end class Invitation …
0
votes
2 answers

ActiveRecord .exists?() and default_scope weirdness

We have a default_scope on our User class which restricts users to a set of companies: class User < ActiveRecord::Base default_scope do c_ids = Authorization.current_company_ids includes(:companies).where(companies: { id: c_ids }) end …
0
votes
0 answers

Odd behavior with has_many :through and default_scope

We've got our basic has_many through: for Users and Companies. class User < ActiveRecord::Base extends CompanyMethods has_many :company_users has_many :companies, through: company_users has_many :orders end class CompanyUser <…
0
votes
1 answer

Effects of Rail's default_scope on performance

Can default_scope when used to not order records by ID significantly slow down a Rails application? For example, I have a Rails (currently 3.1) app using PostgreSQL where nearly every Model has a default_scope ordering records by their name: …
0
votes
2 answers

Why isn't this default_scope working?

I'm trying to set a default scope so that Users where notified: true are soft-deleted. notified is a boolean data column. This is what I've tried: class User < ActiveRecord::Base default_scope { where('notified != ?', true) } #... end But this…
Joe Morano
  • 1,055
  • 7
  • 41
  • 95
0
votes
3 answers

How to override default_scope in client_side_validations gem?

I have default_scope on most of my models that scopes by current_user.company. I'm using the client_side_validations gem on my sign up page which means there is no tenant set. When the uniqueness validator runs on @user.email the default_scope…
0
votes
1 answer

Yii framework reset defaultScope

In Yii framework, how can I disable defaultScope ? I tried with resetScope(false) and resetScope(true) but to no avail. Any help would be really appreciated.
user1244197
  • 61
  • 2
  • 11
0
votes
1 answer

default_scope doesnt work with association models's scopes

I have a model setup like the following: class Subject has_one :background_job, as: :backgroundable_job has_many :sub_subjects default_scope { includes(:background_job).where( background_jobs: { backgroundable_job_id: nil } ) } end The…
Alexandra
  • 51
  • 3
0
votes
1 answer

Force where in ActiveRecord model in Rails

Is there any way to force a where statement to be included in every SELECT-statement I do with Active Record? I'm using Rails 4. For example: If I call: Product.where(name: 'Fishing rod') Then I want Rails to generate a SQL query like the…
Timm
  • 985
  • 6
  • 18
0
votes
1 answer

Rails 3.0 - default_scope with Proc didn't work

I have big project written on Rails 3.0. Biggest model of that project is Questinary. It's inherited from ActiveRecord::Base as all typical models. Many parts of that model taked out into separate models inherited from Questionary. I have a…
0
votes
1 answer

Rails application trying to set default_scope of a model

In this application the model Resource_Estimations are done by Companies. I wish to have a default sort order for the Resource_Estimations of the Company name. Company Model class Company < ActiveRecord::Base include…
user1854802
  • 388
  • 3
  • 14
0
votes
1 answer

acts_as_list in multi-tenant application - how do I set the scope?

Question I'm using acts_as_list in a multi-tenant app. I modeled the multi-tenancy using this Railscast: #388 Multitenancy with Scopes (subscription required). I need to ensure my around_filter works so that tenants can't affect other tenants'…
0
votes
0 answers

Rails 4 not respecting the default_scope when using a has_one association

I have a model class that has a default scope, something like this class Avatar self.table_name = 'attachments' belongs_to :user default_scope -> { where(type: 'avatar') } end class User has_one :avatar end I expected User.first.avatar to…
Logan Serman
  • 27,645
  • 25
  • 100
  • 139
0
votes
3 answers

RSpec with multi tenancy - Why is this simple test failing?

What I'm doing I recently implemented multi-tenancy (using scopes) following Multitenancy with Scopes (subscription required) as a guide. NOTE: I am using the dreaded "default_scope" for tenant scoping (as shown in Ryan's Railscast). Everything is…
JoshDoody
  • 387
  • 3
  • 14