named-scope

Is there a way to combine named scopes into a new named scope?

I have class Foo < ActiveRecord::Base named_scope :a, lambda { |a| :conditions => { :a => a } } named_scope :b, lambda { |b| :conditions => { :b => b } } end I'd like class Foo < ActiveRecord::Base named_scope :ab, lambda { |a,b| :conditions => { :a => a, :b => b } } end but I'd prefer to do it in a DRY fashion. I can get th...

Encapsulating SQL in a named_scope

I was wondering if there was a way to use "find_by_sql" within a named_scope. I'd like to treat custom sql as named_scope so I can chain it to my existing named_scopes. It would also be good for optimizing a sql snippet I use frequently. ...

Rails named_scopes with joins

I'm trying to create a named_scope that uses a join, but although the generated SQL looks right, the result are garbage. For example: class Clip < ActiveRecord::Base named_scope :visible, { :joins => "INNER JOIN series ON series.id = clips.owner_id INNER JOIN shows on shows.id = series.show_id", :conditions=>"shows.visi...

How to extract common named_scopes from ActiveRecord models

I have named_scope which is reused in multiple ActiveRecord models. For example: named_scope :limit, lambda {|limit| {:limit => limit}} What is the best practice to extract this code to be shared across models. Is it possible to extract it to a module or should I rather reopen ActiveRecord::Base class? ...

Using scope_builder to conditionally build a named scope in an Active Record model search() method

I'm using Ryan Bates' excellent scope_builder to conditionally build a new named scope to use in a search() method of an Active Record model. The example documentation shows that you can do something like the following: # in product model def self.search(options) scope_builder do |builder| builder.released.visible b...

Get ids of elements in a named scope

In ruby on rails, all has_many :widgets methods generate both a object.widgets method and an object.widget_ids method. The latter method is very useful because it bypasses the creation of ActiveRecord objects and runs a much faster query. The query speed can be improved by using the :select option, but ruby still allocates much more mem...

Variable field name in named_scope?

In a Rails model I am trying to acheive a named_scope that filters on a start_date and end_date. This is easy. But I am going to have to do it on lots of different fields on many occasions. Is this asking for trouble? If so why (SQL injection?) and is there another way to acheive this. named_scope :between, lambda {|start_date, end_da...

A difficult named_scope situation

In my application, Users can start and participate in Discussions. They can also Tag Discussions; when they do so, a Tag is created containing the name of the tag (if it didn't already exist), and a Tagging, which remembers which User tagged which Discussion with what Tag, is created too. So inside the Discussion model we have this: h...

named_scope is creating new objects if I use it with a :join

Disclaimer: My original exposure to Ruby on Rails was literally years ago, and a great many things are new to me now that I'm picking it back up. One of these things is named scopes. They seem great, but I'm not getting the result I expect. Here's a for-instance: class User has_many logs named_scope :logged_in, :joins => ['logs'...

Change a finder method w/ parameters to an association

How do I turn this into a has_one association? (Possibly has_one + a named scope for size.) class User < ActiveRecord::Base has_many :assets, :foreign_key => 'creator_id' def avatar_asset size = :thumb # The LIKE is because it might be a .jpg, .png, or .gif. # More efficient methods that can handle that are OK. ;) se...

ActiveRecord conditions of an association (Rails)

Pretend I have a model, Post which has_many :comments. How can I only display posts that have comments? I am somewhat comfortable with named_scope but I don't know how I can put Post.comments (or self.comments) in the :conditions hash which expects symbols. class Post < ActiveRecord::Base has_many :comments named_scope :with...

Trying to define named_scopes and other ActiveRecord relationships on all models.

I'm trying to define a named_scope for all my models in a Rails application. Presently, I've been able to get close to this by writing an initializer for ActiveRecord::Base and putting regular methods in there. Of course, this offers no real advantage when it comes to creating query chains and is probably the least rails-ey way of getti...

named_scope in rails with a has_many association

Hi all, I am trying to achieve what I think will be a fairly complex query using the magic of Rails without having lots of ugly looking SQL in the code. Since my database is dealing with rather specialised biomedical models I'll translate the following to a more real world scenario. I have a model Book that has_many :chapters and C...

Is there a way of doing filtering joined associations using named scope?

I have the following associated models class Enrollment < ActiveRecord::Base has_many :addresses end class Address < ActiveRecord::Base belongs_to :address_type end Currently I'm using the following (which I think is ugly) to filter out enrollment addresses of a certain address type. class Enrollment < ActiveRecord::Base def l...

Joins across multiple tables with ActiveRecord with named scopes

I love making named scopes for rails. however, I ran into somewhat of a pickle. Ive gotten pretty comfortable using named scopes for joins like so: named_scope :foo, :joins => :bar, :conditions => "bar_attribute = 'something'" Now pretend I have a table called baz which is contains a foreign key from the bar table. I need something l...

problem using sql instead of named scope in rails

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 search method if i use group of named scope then it works ...

Check if model instance falls within named_scope in rails

Assume I have a named scope: class Foo < ActiveRecord::Base named_scope :bar, :conditions => 'some_field = 1' end This works great for queries and I have a bunch of useful named_scopes defined. What I would like is to be able to do this: f = Foo.find(:first) f.some_field = 1 f.is_bar? #=> true The '.bar?' method will simply ret...

Additive Chaining with named_scope

Is there a way to combine scopes in an additive fashion? If I have the scopes User.big_haired and User.plays_guitar I can call User.big_haired.plays_guitar and get all the users who have big hair AND play guitar. Can I write this to get all users who have big hair OR play guitar? I guess I have to add that I realize that you ...

Best way to handle multiple possible queries conditions on an index page

I have a application which stores documents created by users. These documents have authors, creation dates. The authors can have roles. Additionally a given document may get tagged with keywords. There are ~16K documents in the database, and a user may want to view the documents with any combination of this information as a limit. F...

Complicated named_scope: Find users who don't belong to a certain project

I'm trying to create a named scope like User.not_in_project(project) but I can't find the right way. I have Users, Projects and Duties as a join model: class User < ActiveRecord::Base has_many :duties, :extend => FindByAssociatedExtension has_many :projects, :through => :duties end class Duty < ActiveRecord::Base belongs_to :use...