default-scope

default_scope breaks (update|delete|destroy)_all in some cases

混江龙づ霸主 提交于 2020-01-22 18:42:30
问题 I believe this is a bug in Rails 3. I am hoping someone here can steer me in the correct direction. The code posted below, is purely for illustration of this problem. Hopefully this does not confuse the issue. Given I have a Post model, and a Comment model. Post has_many Comments, and Comment belongs_to Post. With a default_scope set on the Post model, defining joins() and where() relations. In this case where() is dependent on joins(). Normally Posts wouldn't be dependent on Comments. Again,

Rails 4 Relation is not being sorted: 'has_many' association w/ default scope, 'includes' and 'order' chained together

跟風遠走 提交于 2019-12-24 14:46:45
问题 I am trying to use a default scope to impose a sort order on the model QuizCategoryWeight. The goal is to get @possible_answer.quiz_category_weights to return the weights in sorted order. Update: I have narrowed the problem down to the fact that default scopes seem to work for me as long as they just have an 'order' method but not when the 'includes' method is chained with the 'order' method. However, this chaining does work for named scopes. Could it be my development environment? Or is this

Rails 3 default scope, scope with override

霸气de小男生 提交于 2019-12-08 19:17:03
问题 I have a situation where the behavior of an existing app is changing and it's causing me a major headache. My app has Photos. Photos have a status: "batch", "queue", or "complete" . All the existing Photos in the app are "complete". 99% of the time I need to only show complete photos, and in all of the existing codebase I need every call to Photos to be limited to only complete photos. However, in the screens related to uploading and classifying photos I need to be able to fairly easily

Rails 4 - acts_as_votable, change default sort order

一世执手 提交于 2019-12-08 15:16:32
I have a Rails app that allows voting on some models. In one of my models I want to change the default_scope so that objects that has the most votes is displayed first. I have tried the following in my model file: def self.default_scope order('votes_for.size DESC') end This gives me the following error: SQLite3::SQLException: no such column: votes_for.size: SELECT "solutions".* FROM "solutions" WHERE "solutions"."competition_id" = ? ORDER BY votes_for.size DESC I wonder if there is a way the change the default default sort order, what I'm doing is obviously not working. Solutions for making it

Setting default_scope according to some criteria

 ̄綄美尐妖づ 提交于 2019-12-08 09:00:26
问题 I'm trying to set default scope according to some criteria determined by ana ActionController before_filter. In controller: before_filter :authorize ... def authorize if some_condition @default_scope_conditions = something elsif another_condition @default_scope_conditions = something_else end end Inside the ActiveRecord default_scope :conditions => @default_scope_conditions But it doesn't seem to work, the before filter gets called but the default_scope doesn't get set. Could you please

Rails 4 - acts_as_votable, change default sort order

醉酒当歌 提交于 2019-12-08 03:47:17
问题 I have a Rails app that allows voting on some models. In one of my models I want to change the default_scope so that objects that has the most votes is displayed first. I have tried the following in my model file: def self.default_scope order('votes_for.size DESC') end This gives me the following error: SQLite3::SQLException: no such column: votes_for.size: SELECT "solutions".* FROM "solutions" WHERE "solutions"."competition_id" = ? ORDER BY votes_for.size DESC I wonder if there is a way the

Eager loading of deleted records with paranoia's default scope

我是研究僧i 提交于 2019-12-07 10:58:28
问题 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 => { :deleted_at => nil } So in effect, I have these (simplified) models: class Product has_many :orders default_scope :conditions => { :deleted_at => nil } end class Order belongs_to :product end What I'm trying to achieve is to eager-load the products

Eager loading of deleted records with paranoia's default scope

早过忘川 提交于 2019-12-05 15:47:48
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 => { :deleted_at => nil } So in effect, I have these (simplified) models: class Product has_many :orders default_scope :conditions => { :deleted_at => nil } end class Order belongs_to :product end What I'm trying to achieve is to eager-load the products when accessing orders: Order.includes(:product) This (from How to use unscoped on associated relations in

default_scope breaks (update|delete|destroy)_all in some cases

扶醉桌前 提交于 2019-12-04 02:40:41
I believe this is a bug in Rails 3. I am hoping someone here can steer me in the correct direction. The code posted below, is purely for illustration of this problem. Hopefully this does not confuse the issue. Given I have a Post model, and a Comment model. Post has_many Comments, and Comment belongs_to Post. With a default_scope set on the Post model, defining joins() and where() relations. In this case where() is dependent on joins(). Normally Posts wouldn't be dependent on Comments. Again, I just want to give a simple example. This could be any case when where() is dependent on joins().

Owner-filtered model objects on Rails 3

百般思念 提交于 2019-12-02 07:34:55
问题 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 model. I've read some solutions, but none works, basically any of them says that you can use session when declaring default_scope. This is my declaration for the scope: class MyModel < ActiveRecord::Base default_scope { where(:owner_id => session[