named-scope

Pass current_scopes on to search

时光毁灭记忆、已成空白 提交于 2019-12-06 15:41:59
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 a form but after googling I'm not sure that is true. Search form <%= search_form_for @search, :html =>

Joins across multiple tables with ActiveRecord with named scopes

橙三吉。 提交于 2019-12-06 15:12:20
I love making named scopes for rails. however, I ran into somewhat of a pickle. Ive gotten pretty comfortable using named scopes for joins like so: named_scope :foo, :joins => :bar, :conditions => "bar_attribute = 'something'" Now pretend I have a table called baz which is contains a foreign key from the bar table. I need something like this: named_scope :foo, :joins => (:bar => :baz), :conditions => "bar.id = baz.bar_id AND baz_attribute = 'something_else'" How possible is this? thanks You are not that far off. This should work for you: named_scope: foo, :joins => {:bar => :baz}, :conditions

will paginate miscounting oddness with named scopes

戏子无情 提交于 2019-12-05 19:29:45
I recently broke up my query into 4 named scopes to make it easier to re-sort and will paginate which otherwise always worked fine now has problems calculating the number of pages. named_scope :loc, lambda { |id| { :conditions => ['location_id = ?', id ] } } named_scope :datem, lambda { |*args| { :joins => :scannables, :conditions => [ "scannables.bookdate BETWEEN ? and ?", (args[0].to_date || 3.days.from_now), (args[0].to_date+(args[1] || 3)) ], :group => 'scannables.hostel_id', :having => 'SUM(scannables.available) > ' + ((args[1] || 3).to_i-1).to_s } } named_scope :order_by_external_desc,

Is it possible to invert a named scope in Rails3?

假装没事ソ 提交于 2019-12-05 18:02:41
问题 In my Rails3 model I have these two named scopes: scope :within_limit, where("wait_days_preliminary <= ? ", ::WAIT_TIME_LIMIT.to_i ) scope :above_limit, where("wait_days_preliminary > ? ", ::WAIT_TIME_LIMIT.to_i ) Based on their similarity, it would be natural for me to define the second by inverting the first. How can i do that in Rails? 回答1: Arel has a not method you could use: condition = arel_table[:wait_days_preliminary].lteq(::WAIT_TIME_LIMIT.to_i) scope :within_limit, where(condition)

Named_scope in rails unique records?

♀尐吖头ヾ 提交于 2019-12-05 03:28:44
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 may do it and I'm using that for now. If anyone has any comments regardless that'd be great. You

Check if model instance falls within named_scope in rails

落花浮王杯 提交于 2019-12-04 19:41:20
问题 Assume I have a named scope: class Foo < ActiveRecord::Base named_scope :bar, :conditions => 'some_field = 1' end This works great for queries and I have a bunch of useful named_scopes defined. What I would like is to be able to do this: f = Foo.find(:first) f.some_field = 1 f.is_bar? #=> true The '.bar?' method will simply return true or false if the model instance falls within the named scope. Is there anyway to do this without writing an 'is_bar?' method even though I've already written a

Rails 3: How to create a named scope based on Controller's method?

谁都会走 提交于 2019-12-04 17:00:43
In my ApplicationController I have the demo_mode? method (which returns true when the currently logged in user type is "demo"). Post model has the publisher_id field, which refers to Users table. User has the user_type field, one of the possible values of which is "demo". Bottom line: post p was published by a "demo" user if and only if: User.find(p.publisher_id).user_type == "demo" I would like to create a named scope Post.relevant that will return: all posts that were published by "demo" users, if demo_mode? == true all posts that were published by non "demo" users, if demo_mode? == false

Rails named_scopes with joins

≡放荡痞女 提交于 2019-12-04 15:48:14
问题 I'm trying to create a named_scope that uses a join, but although the generated SQL looks right, the result are garbage. For example: class Clip < ActiveRecord::Base named_scope :visible, { :joins => "INNER JOIN series ON series.id = clips.owner_id INNER JOIN shows on shows.id = series.show_id", :conditions=>"shows.visible = 1 AND clips.owner_type = 'Series' " } (A Clip is owned by a Series, a Series belongs to a Show, a Show can be visible or invisible). Clip.all does: SELECT * FROM `clips`

will_paginate with named_scopes

杀马特。学长 韩版系。学妹 提交于 2019-12-04 08:12:53
问题 I'm using will_paginate for pagination, which has been working well so far, except for this one thing. If I try to paginate a scope, for instance class User < ActiveRecord::Base named_scope :scope, lambda { etc } end User.scope.paginate({:page => params[:page], :per_page => 10}) That will tell me paginate is an undefined method. I'd rather not have to use a second solution for only this scope, is there something I can do here? 回答1: Lowgain, klew's version should work out of the box. In your

Is it possible to invert a named scope in Rails3?

China☆狼群 提交于 2019-12-04 03:49:24
In my Rails3 model I have these two named scopes: scope :within_limit, where("wait_days_preliminary <= ? ", ::WAIT_TIME_LIMIT.to_i ) scope :above_limit, where("wait_days_preliminary > ? ", ::WAIT_TIME_LIMIT.to_i ) Based on their similarity, it would be natural for me to define the second by inverting the first. How can i do that in Rails? Arel has a not method you could use: condition = arel_table[:wait_days_preliminary].lteq(::WAIT_TIME_LIMIT.to_i) scope :within_limit, where(condition) # => "wait_days_preliminary <= x" scope :above_limit, where(condition.not) # => "NOT(wait_days_preliminary <