named-scope

will paginate miscounting oddness with named scopes

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

How do I make named_scope work properly with a joined table?

Here's my situation. I have two tables: pledges and pledge_transactions. When a user makes a pledge, he has only a row in the pledges table. Later when it comes time to fulfill the pledge, each payment is logged in my pledge_transactions table. I need to be able to query all open pledges which means that the sum of the amounts in the t...

has_many and sum named_scope

I have this situation: Stories has many Tasks Tasks have an integer called hours_left I need a named scope to find Stories which all its tasks has more than 0 hours left. Based on this post. I wrote this: class Story has_many :tasks named_scope :uncompleted, { :joins=>["INNER JOIN tasks ON tasks.story_id = stories.id"], ...

Rails: Can joins be merged when chaining scopes?

In a class A I have two scopes, s1 and s2 which both join over a table T using the exact same join columns: named_scope :s1 :joins => "JOIN T on T.id = A.t_id", ...some conditions named_scope :s2 :joins => "JOIN T on T.id = A.t_id", ...some other conditions Now, doing this fails: A.s1.s2.all Error: ActiveRecord::StatementInvali...

Matching available languages to language names

I want to make a language selection dropdown in a site user edit/create page. For this purpose, I have of course translated the site to more than one language. Using I18n.available_languages, I can then get an array of locale codes, like so development environment (Rails 2.3.4) > I18n.available_locales => [:en, :da] Furthermore, I...

basic modification of default model output with scope

I find myself doing the same things over and over again just to make one small modification to standard model output. I have a series of tables that I store information about products, etc. and all of which store prices. The prices are stored in US dollars but the output depends on the currency the user wants which is stored in a thei...

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" } } ...

ActiveRecord named_scope, .scopes

The background to this problem is quite complex and convoluted, and as I am looking for a simple answer, I will leave it by the wayside in explaining my problem, and instead provide this hypothetical situation. If I have a simple ActiveRecord model called Automobile, with named_scopes like below: named_scope :classic, :conditions => { ...

How to test named_scopes and search methods?

When I learned about proxy_options I started using it to test all my named scopes. But then I found myself simply copying the conditions hash straight from the model, so it wasn't really testing the correctness of the results: po = {:conditions => "foo.created_at <= '#{Time.now.beginning_of_week}'"} assert_equal po, Foo.created_this...

How do I re-use named scopes?

Hi I have a named_scope in my User model as following. named_scope :by_gender, lamdba { |gender| { :conditions => { :gender => gender } } } I want to create other two named scopes which re use this one something like, named_scope :male, lambda { by_gender('male') } named_scope :female, lambda { by_gender('female') } Any idea what...

default_scope with :joins and :select

I tried to define a default_scope in the following way: default_scope :joins => :product, :select => "catalog_products.*, products.*" What I'm getting from Rails though is this: SELECT catalog_products.* FROM `catalog_products` INNER JOIN `products` ON `products`.id = `catalog_products`.product_id When I define it as a named_scope...

rails convert class methods to named scope

Hi all. Rails newbie here. I'm trying to get some class methods into named_scopes. My application structure is similar to a blog application with user comments. Each comment model has a score attribute determined by ratings made from other users. I want to be able to have a named scope that returns the top ten users with the largest tota...

Named scope not cooperating with timezone?

Hi guys, A really dodgy problem I've got. Here's my model: class Entry < ActiveRecord::Base default_scope :order => 'published_at DESC' named_scope :published, :conditions => ["published_at < ?", Time.zone.now], :order => 'published_at DESC' belongs_to :blog end Now if I do @entries = Entry.published.paginate_by_blog_id @blog....

Rspec, Model load order, fixtures and named_scope challenge

I have some players and the players have a trade state. Rather than hard code trade states like "active" and "inactive" and then have to go looking for strings, I thought I'd be clever and have a separate TradeState model so that a Player has a trade_state_id (a Player can be in only one trade state at a time). Now, it would be a conve...

"Section" has_many versioned "Articles" -- How can I get the freshest subset?

Hi all! I have a Model called Section which has many articles (Article). These articles are versioned (a column named version stores their version no.) and I want the freshest to be retrieved. The SQL query which would retrieve all articles from section_id 2 is: SELECT * FROM `articles` WHERE `section_id`=2 AND `version` IN ( SELEC...

named scope vs. find_by_sql (specific example)

Just out of curiosity, does anyone know a better way of building the following collection using named scopes (as opposed to find_by_sql)? @available = Workflow.find_by_sql([" SELECT workflows.id FROM workflows WHERE workflows.project_id = ? AND workflows.status < 5 AND ( workflows.created_by = ? OR workflows.id IN ...

Can I create an *un*named scope in Rails?

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} ... MyModel.active.find(...) This works by creating a proxy object which isn't evaluated until later on. What I want to know is if it's possible to create a dynami...

named_scope to order posts by last comment date

Posts has_many Comments I'm using searchlogic which will order by named scopes. So, I'd like a named scope that orders by each post's most recent comment. named_scope :ascend_by_comment, :order => ...comments.created_at??... I'm not sure how to do a :joins and get only the most recent comment and sort by its created_at field, all in...

Named_scope in rails unique records?

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 tihs case there may be three records with the same style, I want this named_scope to only give uniqu...

How to return boolean result in named_scope?

named_scope :incomplete?, lambda { |user_id, todo_id| { :select => 1, :conditions => [ "#{user_id} not in (select user_todos.user_id from user_todos) and #{todo_id} not in (select user_todos.todo_id from user_todos)" ] } } I'm getting a nil result. I want it to return true. What I gotta do!? Also, is there a better w...