default-scope

default_scope with :joins and :select

I tried to define a default_scope in the following way: default_scope :joins => :product, :select => "catalog_products.*, products.*" What I'm getting from Rails though is this: SELECT catalog_products.* FROM `catalog_products` INNER JOIN `products` ON `products`.id = `catalog_products`.product_id When I define it as a named_scope...

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 the rails version? ...

Overriding default_scope in Rails

In my Post.rb model, I have default_scope :conditions => {:deleted => 'false'} But if I try to run Post.find(:all, :conditions => "deleted='false'"), it won't return anything. It's as if the default_scope takes precedence over everything. I want it so that when I do Post.find() it doesn't return deleted posts, but I'd also like to b...

default_scope in rails 3

I know named_scope has been changed to scope in rails 3. How do I perform default_scope in rails 3, I've had a good google but found nothing for defaults scopes. ...

Setting default_scope according to some criteria

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...

Is there a way to break out of the default_scope when using has_many?

I have a tree-like model where in all situations but one, I want to scope the results to only return the roots. class Licence < ActiveRecord::Base default_scope :conditions => { :parent_licence_id, nil } belongs_to :parent_licence, :class_name => 'Licence' has_many :nested_licences, :class_name => 'Licence', :foreign_k...

Problem with default_scope in Rails 3? NoMethodError

I am experiencing a weird bug after adding this to my Alternative-model: default_scope order(:number) On the first page refresh, everything works fine. On subsequent refreshes, I get NoMethodError in Pages#show undefined method `written?' for #<Alternative:0x10455c558> Alternative most assuredly has the written? method. I am runni...

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. However, when looping through these photo albums in an admin panel to display the number of ...

default_scope not working with sessions in my tests

Iam creating an application where a user can have many organisations each organisation has it's own data, things work fine on development, but when I'm in test mode it doesn't work. For example I have a class Tax class Tax < ActiveRecord::Base # This shouln't be neccesary according to documentation before_create :set_organisation_id...

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

Hello, 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_scop...

default_scope and associations

Suppose I have a Post model, and a Comment model. Using a common pattern, Post has_many Comments. If Comment has a default_scope set: default_scope where("deleted_at IS NULL") How do I easily retrieve ALL comments on a post, regardless of scope? This produces invalid results: Post.first.comments.unscoped Which generates the follow...

rails3 default_scope, and default column value in migration

class CreateCrews < ActiveRecord::Migration def self.up create_table :crews do |t| t.string :title t.text :description t.boolean :adult t.boolean :private t.integer :gender_id t.boolean :approved, :default => false t.timestamps end end def self.down drop_table :crews end end ...