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

HOME ยป 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

Related to : How to make a complex named scope play nice with associations and other named scopes [rails]
rails find: using conditions while including same table twice via different named associations

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", :foreign_key => "to_user_id"

Both :user and :post have "is_public" column, indicating that either a single post and/or the entire user profile can be public or private.

My goal is to fetch a list of posts which are public AND whose recipients have public profiles. At the same time, I would like to "include" both sender and recipient info to

rails named scope issues

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 * #{(CurrencyRate.get_rate("USD", (code ||= "USD") ,1))} AS price" } }


The total named scope works but not the currency...


The curre

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

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 Service A and Service B or other service providers who provide either Service A or Service B. Instead of this we'd like to limit it to only providers who provide BOTH services.

My scope looks like this:

named_scope :with_services, lambda { |services| {
:conditions => [" IN (#{{|s|}.join(', ')})

ruby on rails named scopes (searching)

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}%"]} }
if search
self.find(:all, :conditions => [ 'full_name LIKE ?', "%#{search}%"])

but this doesn't work as it gives the following error:

SQLite3::SQLException: no such column: full_name: SELECT * FROM "actors" WH
Ruby on Rails: Nested named scopes

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 :spouse
named_scope :with_spouse, :include => :spouse
class Spouse
has_one :employee

Is there any nice way for me to find a company while including employees and spouses like this:
or is it necessary for me to define another name

How to revert back from named-wrapper -u named to /usr/sbin/named -u named?
How to revert back from named-wrapper -u named to /usr/sbin/named -u named?
After upcp suddently named failed and only works when I use /scripts/ulimitnamed
But now the server feel so slow because of became named-wrapper -u
How to revert back from named-wrapper -u named to /usr/sbin/named -u named?
Thanks alot.

how to join named scopes in rails

I want to join my named scopes as I generate them with an array. how wouldI do that , i can't join named scopes, is there a better way to do this?

 scope :search, ->(attrs_for_search,params) do
   if attrs_for_search.present?
     params_to_search_on = params.keys & attrs_for_search
     params_to_search_on.collect{|x| where("#{x} ILIKE ?",
"%#{params[x]}%")     }
 end[email],{'email => 'jason'})

Privacy Policy - Copyrights Notice - Feedback - Report Violation - RSS 2017 © All Rights Reserved .