named-scope

Can I create an *un*named scope in Rails?

蹲街弑〆低调 提交于 2019-12-10 15:39:45
问题 I know you can create named scopes in Rails, which allow you to specify conditions which can then be built on later: named_scope :active, :conditions => {:active => true} ... MyModel.active.find(...) This works by creating a proxy object which isn't evaluated until later on. What I want to know is if it's possible to create a dynamic un named scope? By which I mean, is there a method 'foo' with which I can go scope = MyModel.foo(:conditions => {:target_id => 4}) and then pass scope around as

Reusing named_scope to define another named_scope

﹥>﹥吖頭↗ 提交于 2019-12-10 13:03:54
问题 The problem essence as I see it One day, if I'm not mistaken, I have seen an example of reusing a named_scope to define another named_scope. Something like this (can't remember the exact syntax, but that's exactly my question): named_scope :billable, :conditions => ... named_scope :billable_by_tom, :conditions => { :billable => true, :user => User.find_by_name('Tom') } The question is: what is the exact syntax, if it's possible at all? I can't find it back, and Google was of no help either.

Empty Scope with Ruby on Rails

时间秒杀一切 提交于 2019-12-10 12:30:38
问题 Following Problem: I need something like an empty scope. Which means that this scope is emtpy, but responds to all methods a scope usually responds to. I'm currently using a little dirty hack. I simply supply "1=0" as conditions. I find this realy ugly, since it hits the database. Simply returning an empty array won't work, since the result must respond to the scoped methods. Is there a better existing solution for this or will I need to code this myself? Maybe some example code could help

Named scopes with inheritance in Rails 3 mapping to wrong table

前提是你 提交于 2019-12-10 10:07:23
问题 I am attempting to use inheritance from a class which has a named scope: Class A < ActiveRecord::Base scope :useful_scope, lambda { |value1, value2| where(:value1 => value1, :value2 => value2) end end Class B < A set_table_name "b" end The problem I'm encountering is that the table name in the sql queries still reference Class A's Table: A.useful_scope("alpha", "beta").to_sql => "SELECT \"a\".* FROM \"a\" WHERE \"a\".\"value1\" = 'alpha' AND \"a\".\"value2\" = 'beta'" B.useful_scope("alpha",

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 ActiveRecord Scope that is the “opposite” of another scope, or is users that “lack” a property

荒凉一梦 提交于 2019-12-08 17:09:44
问题 I have a model for user.rb, in which I define a scope for admins , which is users that have the role of admin through a permissions table. has_many :permissions has_many :roles, :through => :permissions The scope works like this: scope :admins, joins(:permissions).merge(Permission.admin_permissions) I'd also like to make a scope called non-admins or something like that, which is all users that do NOT have the admin role. What's the easiest way to do this? 回答1: An easy way, which would not be

How do I call all comments for a post (workout in this case) where workout.user_id == current_user.id?

白昼怎懂夜的黑 提交于 2019-12-08 10:44:45
问题 I have created a Ruby on Rails app where users can record their workouts and other users can comment on those workouts. I am using a Dashboard resource to aggregate information for current_user. I am trying to display recent comments on a current_user's workouts but can't seem to figure out how to do this correctly. I think I need a named_scope which I am not great at yet. I essentially want the app to loop through the comments table but only return comments on Workouts where workout.user_id

Pass current_scopes on to search

两盒软妹~` 提交于 2019-12-08 05:26:22
问题 I'm using the very helpful has_scope gem. I have an index page created by passing scopes in a link. <%= user_collections_path(@user, got: true) %> On that index page I can query the current_scopes. I have a pretty standard ransack search form on that page but using it returns results without any scopes. How can I pass the current_scopes on to the search? I tried using a hidden field but couldn't get it to work. I thought if I could pass the scopes in a link I ought to be able to pass them in

Rails 4 with has_scope: fails silently

给你一囗甜甜゛ 提交于 2019-12-08 04:16:22
问题 I have a Rails 4.0.0 app and want to include the has_scope gem. Very simple and straight resources: Model: class Thing < ActiveRecord::Base resourcify ... belongs_to :client validates_presence_of :name scope :by_client, -> client { where(:client => client)} scope :by_name, -> name { where(:name => name)} Controller: class ThingsController < ApplicationController include Pundit before_filter :authenticate_user! before_action :set_thing, only: [:show, :edit, :update, :destroy] after_action

Named_scope in rails unique records?

我只是一个虾纸丫 提交于 2019-12-06 23:01:21
问题 Is it possible to have named_scope return records unique for a certain column? e.g named_scope :unique_styles, :order =>"title desc", :limit => 3 That will give me three styles but what if I want to make sure the title is different? In this case there may be three records with the same style, I want this named_scope to only give unique values of title. So ["style 1", "style 1", "style 1"] isn't possible, it'll force itself to give ["style 1", "some style 2", "maybe another 3"] i think group