default-scope

Owner-filtered model objects on Rails 3

柔情痞子 提交于 2019-12-02 06:46:05
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[:user_id]) } ... end Simple, right?. But it fails saying that method session does not exists . Hope you

rails3 default_scope, and default column value in migration

余生颓废 提交于 2019-11-29 09:36:44
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 class Crew < ActiveRecord::Base has_many :users, :through => :crew_users belongs_to :user default_scope where(:approved => true) end When I go to console, and create a new record, the "approved" property is set to true, why ? How I can set it automatically to the default value (false) like shown in my migration file ?

Overriding default_scope in Rails

纵饮孤独 提交于 2019-11-28 17:56:23
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 be able to access them if I need to. What needs to be changed in either my query or my Rails model? Thanks. bdon with_exclusive_scope is protected , so you have to create a class method: def self.include_deleted_in Event.with_exclusive_scope { yield } end then in

How can i have rspec test for my default scope

孤街醉人 提交于 2019-11-28 08:19:18
my model has default_scope(:order => 'created_at' ) my tests (rspec, factory girl, shoulda, etc.) are: require 'spec/spec_helper.rb' describe CatMembership do context "is valid" do subject { Factory.build(:cat_membership) } it { should be_valid } it { should belong_to :cat} it { should belong_to :cat_group} it { should have_db_column(:start_date)} it { should have_db_column(:end_date)} end end I would rather have this tested using a query and checking the results, but if you really must do it, one possible solution would be something like this for Rails 3: CatMembership.scoped.to_sql.should ==

rails3 default_scope, and default column value in migration

人盡茶涼 提交于 2019-11-28 03:20:29
问题 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 class Crew < ActiveRecord::Base has_many :users, :through => :crew_users belongs_to :user default_scope where(:approved => true) end When I go to console, and create a new record, the "approved" property is set to true,

default_scope and associations

被刻印的时光 ゝ 提交于 2019-11-27 22:44:53
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 following queries: SELECT * FROM posts LIMIT 1; SELECT * FROM comments; Instead of: SELECT * FROM posts LIMIT 1; SELECT * FROM comments WHERE post_id = 1; Running: Post.first.comments Produces: SELECT * FROM posts LIMIT 1; SELECT * FROM comments WHERE deleted_at IS NULL AND

Rails 4 default scope

こ雲淡風輕ζ 提交于 2019-11-27 17:47:03
In my Rails app have a default scope that looks like this: default_scope order: 'external_updated_at DESC' I have now upgraded to Rails 4 and, of course, I get the following deprecation warning "Calling #scope or #default_scope with a hash is deprecated. Please use a lambda containing a scope.". I have successfully converted my other scopes but I don't know what the syntax for default_scope should be. This doesn't work: default_scope, -> { order: 'external_updated_at' } Luke Should be only: class Ticket < ActiveRecord::Base default_scope -> { order(:external_updated_at) } end default_scope

default_scope and associations

╄→尐↘猪︶ㄣ 提交于 2019-11-27 04:36:32
问题 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 following queries: SELECT * FROM posts LIMIT 1; SELECT * FROM comments; Instead of: SELECT * FROM posts LIMIT 1; SELECT * FROM comments WHERE post_id = 1; Running: Post

How can i have rspec test for my default scope

北城余情 提交于 2019-11-27 02:14:51
问题 my model has default_scope(:order => 'created_at' ) my tests (rspec, factory girl, shoulda, etc.) are: require 'spec/spec_helper.rb' describe CatMembership do context "is valid" do subject { Factory.build(:cat_membership) } it { should be_valid } it { should belong_to :cat} it { should belong_to :cat_group} it { should have_db_column(:start_date)} it { should have_db_column(:end_date)} end end 回答1: I would rather have this tested using a query and checking the results, but if you really must

Why is using the rails default_scope often recommend against?

感情迁移 提交于 2019-11-27 00:03:53
Everywhere on the internet people mention that using the rails default_scope is a bad idea, and the top hits for default_scope on stackoverflow are about how to overwrite it. This feels messed up, and merits an explicit question (I think). So: why is using the rails default_scope recommended against? wrtsprt Problem 1 Lets consider the basic example: class Post < ActiveRecord::Base default_scope { where(published: true) } end The motivation to make the default published: true , might be to make sure you have to be explict when wanting to show unpublished (private) posts. So far so good. 2.1.1