How to make a complex named scope play nice with associations and other named scopes [rails]

Category : Databases

I have the following named scope on

class RentableItem < ActiveRecord::Base
named_scope :available_at, lambda{ |starts_at, ends_at| {
:select => "t.*",
:from => "(SELECT ri.*, COALESCE(c1.start_date, '#{starts_at}') AS EarliestAvailable,
COALESCE(c2.end_date, '#{ends_at}') AS LatestAvailable
FROM rentable_items ri
LEFT OUTER JOIN contracts c1 ON ( = c1.rentable_item_id AND c1.start_date BETWEEN '#{starts_at}' AND '#{ends_at}')
LEFT OUTER JOIN contracts c2 ON ( = c2.rentable_item_id AND c2.end_date BETWEEN '#{star

View Replies

Related to : How to make a complex named scope play nice with associations and other named scopes [rails]

How do I make an exclusive rather than inclusive query with named scopes in Rails?
Category : Databases

I've created an application that finds service providers based on multiple services passed to it. This is done via has many_through so there is a join object. Basically, if a user asks for service providers with Service A and Service B my scope returns service providers who offer either BOTH Serv

View Replies

ruby on rails named scopes (searching)
Category : Programming Languages

I have a named scope (name) combination of first and last name and I'm wanting to use this in a search box.

I have the code below:

named_scope :full_name, lambda { |fn| {:joins => :actor, :conditions => ['first_name LIKE ? OR second_name LIKE ?', "%#{fn}%", "%#{fn

View Replies

Ruby on Rails: Nested named scopes
Category : Development Tools & Services

Is there any way to nest named scopes inside of each other from different models?


class Company
has_many :employees
named_scope :with_employees, :include => :employees
class Employee
belongs_to :company
belongs_to :s

View Replies

rails find: using conditions while including same table twice via different named associations
Category : Development Tools & Services

I have posts which are sent by users to other users. There are two models - :post and :user, and :post has the following named associations:

belongs_to :from_user, :class_name => "User", :foreign_key => "from_user_id"
belongs_to :to_user, :class_name => "User", :fore

View Replies

rails named scope issues
Category : Databases

I have two named scopes... both which work separately, but when combined do not work.

named_scope :total, :select => "COUNT(*) as days, AVG(price) as price, SUM(price) AS total", :group => :parent_id
named_scope :currency, lambda { |code| { :select => "*, price *

View Replies

What's the significance of named scope in Rails?
Category : Programming Languages

Before going for details.

Question 1:-- What's the meaning of scope here (ie named *scope)?*

what's the benefits of using named scope?


from Agile Development with Rails book:--

class Order < ActiveRecord::Base

View Replies

Rails 4 named scope with record always at end
Category : Programming Languages
Is there a way to order records alphabetically, excluding one record, which I want at the end? class Category < ActiveRecord::Base default_scope -> { order(:name) }endI always want the category named 'Other' to be at the end.

View Replies

Can I create an *un*named scope in Rails?
Category : Programming Languages

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}

This works by creating a proxy object which i

View Replies

problem using sql instead of named scope in rails
Category : Programming Languages

I have a method "search" in my model, which depending upon the various parameters passed runs an sql query in which i am joining seven tables. but when i am using this method with another named scope then error is shown "undefined method call for array".

but when instead of this searc

View Replies

Named scope in Sunspot search in rails 3
Category : Web Design

i am using the Sunspot search from multiple models which are tagged under a name.,Blogpost) do with :tag_ids, params[:id] end

How to apply named_scoped like recent ,popularity which are common for all of the 2 models inthe Sunspot search

View Replies

2012 / 2017 Copyrights BigHow , All Rights Reserved .