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
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
  • 29,447
  • 27
  • 102
  • 141
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
  • 417
  • 1
  • 4
  • 14
0
votes
1 answer

How do I set a default_scope for a has_many :through association based on the join table?

My question How do I create a default_scope for Users such that it limits visibility to only those users who have a tenants_users record for the current tenant (tenants_users.tenant_id == Tenant.current_id) in the join table for the has_many…
JoshDoody
  • 417
  • 1
  • 4
  • 14
0
votes
1 answer

Default scope for production but not in development

I have a model which I want to limit the results to those of a particular client so client number 1 (my database contains other client data too) so I have a scope like this: default_scope { where( :owner_id => 1, :someother_criteria =>…
0
votes
1 answer

different default_scopes for searching and saving in rails (or a better implementation than the one I have)?

I have a rails app with 2 types of users, authenticated and unauthenticated (separated by a email_authenticated:boolean in the database). when I create a user I want it to be unauthenticated but every time I perform any function I want to perform…
Mike H-R
  • 7,726
  • 5
  • 43
  • 65
0
votes
2 answers

Access Controller Variable on default_scope

My app is based on the subdomain. Each manager has it's own subdomain that should show only his auctions. I have: class Lot < ActiveRecord::Base belongs_to :auction end class Auction < ActiveRecord::Base has_many :lots belongs_to…
0
votes
0 answers

ActiveModel::MissingAttributeError in rails 3.2.12 with default_scope

We want to use default_scope to be flexible with the columns shown on customer index page. For example, if a user should not see customer phone#, then we remove phone from default_scope and want to leave the phone blank on index page. We got this…
user938363
  • 9,990
  • 38
  • 137
  • 303
0
votes
1 answer

Rails 3 multiple default_scope alphanumeric

I have a column in my table that are codes. They are in the format of AAA-XXXX-YYY where AAA is Alphabetical XXX is Numeric YYY is Numeric I want to use an alphabetical sort on AAA, then numeric sort on XXXX, then numeric sort on YYY for my…
0
votes
2 answers

Default scope ignoring dynamic value in condition

In my Activity model, I have a default scope: default_scope where(:subject_id => Log.get_subject_id) Problem is in Log.get_subject_id, default value is 0. Here is my Log model: @@subject_id = 0 def self.set_subject_id(val) @@subject_id =…
Petr Brazdil
  • 231
  • 2
  • 10
0
votes
1 answer

Adding default_scope to every model in Rails 3

I'm trying to add a "default_scope" for all models in my project: default_scope where(:deleted => false) Is there any way to add this default_scope for all models automatically? Maybe using Modules?
0
votes
1 answer

default_scope with :include option rails 3.2

i am upgrading a rails3 app to 3.2.6 i have some issues with the deprecated default_scope with options. I have a couple of models Campaigns and Sites with many-many relationship through campaign_sites. class Campaign < ActiveRecord::Base …
venkatareddy
  • 755
  • 5
  • 14
-1
votes
1 answer

How to unscope joins in RoR 4

Trying to unscope joins. Example: class MyModel < ActiveRecord::Base default_scope -> { joins("JOIN other_table ON other_table.this_table_id = this_table.id") } scope :without_relation -> { unscope(:joins) } end The problem is that it unscope…
Vanuan
  • 31,770
  • 10
  • 98
  • 102
1 2 3 4 5
6