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

is it possible to have conditional default scope in rails?

I am on rails 3.2.21, ruby version is 2.0 My requirement is to have role based conditional default scope for a particular model. eg consider role variable as an attribute of logged in user if role == 'xyz' default_scope where(is_active:…
srikant
  • 260
  • 3
  • 11
0
votes
2 answers

Overriding the default_scope

Could someone help me to find out how to override default_scope. In my view I have to show all matches, not only { where("match_date >= now()") } I need to display all matches. I have some reasone to use default_scope. I am very new in Rails. I…
0
votes
2 answers

Best way to override named_scope for has_many associations in Rails?

Note: I'm using Rails 2.3.8, not 3. I have a Photo model with a default_scope: default_scope :conditions => ["published = ?", true], :order => :position Calling photo_album.photos returns all published photos ordered by position as it should.…
modulaaron
  • 2,856
  • 3
  • 24
  • 28
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
  • 6,367
  • 14
  • 83
  • 137
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,715
  • 10
  • 50
  • 114
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
  • 13
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
  • 1,005
  • 8
  • 20
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