named-scope

How to use variable named scopes with attributes of associated models OTHER than id

Hi, I'm having trouble with getting a named scope working using using an attribute of the associated model other than the id column. I have a Firm model which has a city_id column. I also have a City model with a name column. I want to get restful urls like this so as to make use of the has_scope gem and have skinny controllers http:...

How to do a "join" with an anonymous scope in ruby

Hey guys (and girls ^^) !!! Does sommebody knows how to do a "join" with an anonymous scope in ruby ??? With a named scope you just have to add ":joins =>....." but i can't really find the way to do it with anonymous ones ... . Thx in advance for the help ;) ...

Design: How to declare a specialized memory handler class

On an embedded type system, I have created a Small Object Allocator that piggy backs on top of a standard memory allocation system. This allocator is a Boost::simple_segregated_storage<> class and it does exactly what I need - O(1) alloc/dealloc time on small objects at the cost of a touch of internal fragmentation. My question is how ...

named_scope and substings

I have a named_scope in rails that finds episodes by there directors given name named_scope :director_given, lambda { |dr| {:joins => :director, :conditions => ['given = ?', dr]} } It works great but I would like it to also work on substrings one the name. e.g. instead of having to search for 'Lucy' you could just search 'Lu'. P.S....

Ruby on Rails: using nested named_scopes

I just got referred to stackoverflow by a friend here to help with a problem I am having. I am fairly new to ruby on rails and I am working on a collaborative project where we have a script (medal_worker.rb) that is scheduled to run at a fixed intervals to award people various medals based on various participation and success on our web...

Passing arguments to scope_procedure in searchlogic

I'd like to use searchlogic's scope_procedure feature like so class MyModelObject < ActiveRecord::Base scope_procedure :my_scope_proc, lambda { |p1, p2| { :conditions => "p1 >= #{p1} AND p2 < #{p2}" }} end Then, I am doing the search: scope = MyModelObject.search(:my_scope_proc => true) scope.all The above code obviously doesn't...

named_scope or find_by_sql?

I have three models: User Award Trophy The associations are: User has many awards Trophy has many awards Award belongs to user Award belongs to trophy User has many trophies through awards Therefore, user_id is a fk in awards, and trophy_id is a fk in awards. In the Trophy model, which is an STI model, there's a trophy_type colu...

Using named_scopes on the join model of a has_many :through

Hi folks. I've been beating my head against the wall on something that on the surface should be very simple. Lets say I have the following simplified models: user.rb has_many :memberships has_many :groups, :through => :memberships membership.rb belongs_to :group belongs_to :user STATUS_CODES = {:admin => 1, :member => 2, :invi...

will_paginate with named_scopes

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

Translate SQL statement into named_scope?

How can I translate this SQL into a named_scope? Also, I want the total comments param to be passed through a lambda. "select users., count() as total_comments from users, comments where (users.id = comments.user_id) and (comments.public_comment = 1) and (comments.aasm_state = 'posted') and (comments.forum_user_id is null) group by user...

Use named_scope to find number of associated rows (Ruby on Rails + Searchlogic question)

Let's say I have: class ForumTopic < ActiveRecord::Base has_many :forum_posts named_scope :number_of_posts, ?????? end class ForumPost < ActiveRecord::Base belongs_to :forum_topic end What should I put in ????? to allow searchlogic query like: ForumTopic.descend_by_number_of_posts Any help would be very greatly appreciated! ...

How to filter results by multiple fields?

I am working on a survey application in ruby on rails and on the results page I want to let users filter the answers by a bunch of demographic questions I asked at the start of the survey. For example I asked users what their gender and career was. So I was thinking of having dropdowns for gender and career. Both dropdowns would defau...

named_scope + average is causing the table to be specified more then once in the sql query run on postgresql

I have a named scopes like so... named_scope :gender, lambda { |gender| { :joins => {:survey_session => :profile }, :conditions => { :survey_sessions => { :profiles => { :gender => gender } } } } } and when I call it everything works fine. I also have this average method I call... Answer.average(:rating, :include => {:survey_sessi...

rails named_scope as an extension to AR::Base

class SomeModel < ActiveRecord::Base named_scope :recent, lambda { { :conditions => ['created_at > ?', 1.week.ago] } } end I want to extend the AR::Base class to have this named_scope for all models, how I can do this ? ...

executing named_scoped only when there are present params

Hi have a model like this: class EventDate < ActiveRecord::Base belongs_to :event named_scope :named, lambda { | name | { :joins => { :event => :core}, :conditions => ["name like ?", "%#{ name }%"] }} named_scope :date_range, lambda { | start, length | { :conditions => ["day >= ? AND day <= ?", start, date + (lengt...

Using named_scope with counts of child models

Hi, I have a simple parent object having many children. I'm trying to figure out how to use a named scope for bringing back just parents with specific numbers of children. Is this possible? class Foo < ActiveRecord::Base has_many :bars named_scope :with_no_bars, ... # count of bars == 0 named_scope :with_one_bar, ... # cou...

How to translate find_by_sql statement into named_scope?

How do I translate the following into a named_scope? def self.commentors(cutoff=0) return User.find_by_sql("select users.*, count(*) as total_comments from users, comments where (users.id = comments.user_id) and (comments.public_comment = 1) and (comments.aasm_state = 'posted') and (comments.talkboard_user_id is null) ...

How to Resolve MySQL Error? Chaining Named Scopes

I'm trying to chain two named_scopes in my User model. The first: named_scope :commentors, lambda { |*args| { :select => 'users.*, count(*) as total_comments', :joins => :comments, :conditions => { :comments => { :public_comment => 1, :aasm_state => 'posted', :talkboard_user_id => nil} }, :group => 'user...

How transform this find_by_sql to named_scope?

How can I possibly turn into named_scope? def self.hero_badge_awardees return User.find_by_sql("select users.*, awards.*, badges.badge_type from users, awards, badges where awards.user_id = users.id and badges.id = awards.badge_id and badges.badge_type = 'HeroBadge'") end ...

What's the significance of named scope in Rails?

Before going for details. Question 1:-- What's the meaning of scope here (ie named *scope)?* what's the benefits of using named scope? Now:- from Agile Development with Rails book:-- class Order < ActiveRecord::Base named_scope :last_n_days, lambda { |days| {:conditions => ['updated < ?' , days] } } named_scope :checks, :con...