named-scope

How to add existing named_scope to anonymous scope?

Hi, I have a model: class Shirt < ActiveRecord::Base named_scope :red, :conditions => { :color => 'red' } named_scope :blue, :conditions => { :color => 'blue' } named_scope :xl, :conditions => { :size => 'xl' } end I forgot, how to easy add named scope to existing anonymous scope: scope = Shirt.scoped({}) #and how to add ie...

Empty Scope with Ruby on Rails

Hi there 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, sin...

named_scope error on belongs_to association

I am getting a named_scope error, am i trying to use it incorrectly? class Owner < ActiveRecord::Base has_many :dogs named_scope :is_fit?, :conditions => { :age => 16..40 } end class Dog < ActiveRecord::Base belongs_to :owner def is_owner_fit? owner.is_fit? end end undefined method `is_fit?' for #<ActiveRecord::Associ...

How can I sort by the average of a polymorphic child's attribute with ActiveRecord 2.3?

I have this schema: class Comment has_many :ratings, :as => :target end class Rating belongs_to :target, :polymorphic => true end I want to write a named scope that will sort comments by their average rating, without fetching the whole list of comments and then fetching all their ratings. I think I need to use :include and :grou...

How is it that find_by_id, etc. work on an ActiveRecord array?

Forgive me if I've got my terminology wrong; I am still quite new to Ruby and Rails. For school I am working on an RoR project as part of a team. I was pair programming with a teammate, who actually happens to be experienced in RoR; and I wrote something like the following: d = self.deliverables.find_all_by_lifecycle_id(self.lifecycle_...

Tricky ActiveRecord + SQL Query (Refactor)

Here's my current named_scope. It returns an aggregate result from a number of rows. Sample result from 300 rows: Total Points: 3000 Wins: 31 Losses: 11 Here is the code: named_scope :open_for, lambda { |sportable_id, sportable_type, days_ago| { :conditions => ['sportable_id = ? and sportable_type = ? and game_time > ? ', sport...

named_scopes and all

I'm on Rails 2.3.5. Inside my application a user could book one or more time slots for a specific day. I've defined this named_scope in my Slot model: # day time slots named_scope :for_day, lambda { |day| if !day.blank? { :conditions => ['day_of_week = ?', day] } end } I use that to retrieve all the "time slots" av...

Rails Nested Form - Filter by Current User, Lesson-Questions-Answers-Users

Hi All, I have a nested form that is based on the following model -- A lesson has many questions, each question has many answers, and answers belong to users. I am developing a nested form, so that a new user can review the questions and post answers. If a user has entered answers in the past, I want those to show up; otherwise show ...

Override a scope in rails 3

I want to override an existing scope to add an extra condition to it. I've shown my attempt to do this using alias_method. Unfortunately this approach doesn't work with scopes, I get an undefined method error. How do I do it with scopes? module Delayed module Backend module ActiveRecord class Job < ::ActiveRecord::Base ...

Confused using "using" Statement C#

According to MSDN Library using Statement (C# Reference) Defines a scope, outside of which an object or objects will be disposed. But I got this code posted here by some user and I got confused about this: (please see my comment on the code) using (OleDBConnection connection = new OleDBConnection(connectiongString)) { ...