Wow, and I thought working with the DB couldn't get any easier, but this is seriously cool. Thanks for this excellent overview.
I wonder if these named scopes can be used in conjunction within existing search plugins like acts_as_ferret in one line...
Another great screencast.
Does this essentially make all self.finds obsolete? Will the new conventions be named_scope instead of def self.find_some_criteria?
Very interesting screencast ! Cool new feature ! Thanks for this tip !
Wow, this looks really cool. I especially like the efficiency of the generated SQL query when you concatenated the named_scopes. Really amazing and definately a great feature for bigger, data-filled applications.
Thanks for another great screencast!
Foo:
I would guess that yes, pretty much all finds will be replaced by named scopes in near future. Think of your own experience and some of Ryan's previous screencasts: You shouldn't really have finds anyway as soon as you're using find conditions. Like, say, you'd have find_all_activated instead of find(:all, :conditions => "activated_at IS NOT NULL")
Mauricio:
As far as I can see in the source, there's not too much going on metaprogramming-wise. So I guess it's safe to say that scoping doesn't cause performance issues. And as Marcel points out, named scopes produce really clean and efficient SQL - so no problem there either. Plus, scopes have existed before in Rails (with_scope - see here: http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M001416).
Ryan:
Best episode so far, in my opinion. And not only because it was so long! ;-)
@Adam, I doubt you can use this with acts_as_ferret, but other plugins which end up calling "find" should work. For example, will_paginate just adds a custom paginate class method. You can append this to the search. (Untested).
Product.recent.paginate(:page => 1)
@Foo, this definitely lowers the need to create custom "self.find..." methods. However I would hesitate getting too fancy here. If you have a complicated search which needs to dynamically build up the conditions based on the passed parameters, then I wouldn't use named_scope for this.
@Mauricio, to echo Clemens, I doubt performance will be much of an issue. There's probably a slight overhead on the ruby side to get the scoping stuff figured out, but beyond that it seems efficient.
@Ryan:
in Ruby 1.9 you can use -> operator for lambda and put default values, like this:
named_scope :recent, ->(time = 2.weeks.ago) { {:conditions => ["released_at > ?", time]} }
I've been using has_finder for a while now, and it's been a real pleasure to work with. I'm really glad it's made it into rails.
I have been using edge rails as of late just for this amazing functionality. In addition to the new level of dynamism this feature adds to Rails, it also adds such simplicity.
My favorite has to be simply the Model.all, Model.first, and Model.last shortcuts... cleans up my controllers for sure!
Man, with each new update of Rails, so many new possibilities for refactoring!
I have been trying to do some of the same things using with_scope and some of them were not working correctly (I have an idea why now after watching your screencast) and now it will be so much easier to do what I was trying to do. Thanks for telling me about this new feature!
Indeed a very cool screencast.
Thanks again for this great episode!
I've been using the has_finder gem on which this is based for some time now, and it's really useful. One thing I still struggle with has to do with using named_scopes between models in associations.
In your example you are using 'visible' which is just a simple attribute. What if category had more advanced attributes, like hidden, private, public, read_only and stuff like that. Say Category already has some named_scopes, for example 'free_for_all' which means a category is public but not read-only.
Now it would make sense (maybe not in this example but in general) to be able to use these named_scopes in the Product model, not on instances only, but in named_scopes themselves as well. So Product.in_free_for_all can just use the 'free_for_all' scope in Category. Otherwise you would have to define this 'logic' twice, which isn't very DRY. I hope my explanation is clear enough :)
Of course I know that I could also use Category.free_for_all.map(&:products), but then all products get fetched right away which means sorting them (and other nice stuff finders have) will have to be done in ruby instead of in the DB.
Any 'nice' solutions for this?
A solution I use at the moment is this, but I'm not fully satisfied with it yet, so any comments are very welcome...
class Product << ActiveRecord::Base
belongs_to :category
named_scope :in_free_for_all, lambda {{ :conditions => {:category_id => Category.free_for_all.map(&:id)} }}
end
Which will do 2 queries, which is default for :include in rails 2.1 anyway I believe. I could use :select => 'id' to speed up the query a bit since I only need id's, but in the end, it would be nicest if rails could somehow write a subquery for it. Along the lines of:
(example, doesn't work)
named_scope :in_free_for_all, :conditions => {:category => Category.free_for_all}
should translate to:
Select * from products where category_id in (select id from categories where public = 't' and read_only = 'f')
Any chance of getting behavior like this in the near future?
@Mathijs, currently one named scope can't use another named scope, but I think many people are wanting this feature. There's already a couple tickets along this line:
http://rails.lighthouseapp.com/projects/8994/tickets/57
http://rails.lighthouseapp.com/projects/8994/tickets/223
However, neither of these mention using an association's scope. You may want to add a comment to those tickets about that.
Thanks for a great post! My attention tends to drift after about 7 minutes, so i'll have to come back and watch again a couple times :)
This was a really great Railscast highlighting probably my favorite new feature of Rails. Those SQL queries are great! Can't wait to take advantage of this...
Thanks!
Wow! Truly awesome new feature!
Now I just need to get Hostgator to update to 2.1....
Hello Ryan (or anyone else who knows),
Great screencast. In your demo, you are typing the commands in the console and showing the SQL queries in the console below. How do you isolate the SQL Queries display in the window below? It is quite useful. A screencast summarizing the tips on using Rails Console for these kinds of tricks would be very useful.
Keep up the good work.
Regards,
Bharat
@Bharat: you could just keep a window with ``tail -f log/development.log'' open, that should show the queries. I prefer to have them in the same window though, and have explained how to achieve this here:
http://skionrails.wordpress.com/2008/05/22/loading-additional-files-when-starting-scriptconsole/
Hope that helps. Great work as always Ryan :)
On my website there is a page to view standings for a so called soccer pool. There are member records with a name per pool, that can be left empty. Each member is linked to a user record with a name field as well. In case the name of the member is empty, the user's name is used in the standings.
When using named_scope, I see strange behavior, since the eager loading isn't quite as I expected. I guess things have been changed in Rails 2.1 in this area that have not necessarily have to do with named_scope.
Anyway, in my Member class this scope doesn't work:
named_scope :standings, :include => :user, :order => 'members.position, COALESCE(members.name, users.name)'
The query doesn't join with the users table, so a missing column error is returned. But when I add :conditions => 'users.id = users.id', it works! So is Rails looking in the conditions to see if a associated table is used?
Anybody see my points and have some clues/comments?
Hi
im getting this:
named_scope > NoMethodError
ruby-v
ruby 1.8.5 (2006-08-25) [i486-linux]
rails-v
Rails 2.1.0
gem1.8 list
*** LOCAL GEMS ***
actionmailer (2.1.0, 2.0.2)
actionpack (2.1.0, 2.0.2)
activerecord (2.1.0, 2.0.2)
activeresource (2.1.0, 2.0.2)
activesupport (2.1.0, 2.0.2)
jruby-openssl (0.2.3)
rails (2.1.0, 2.0.2)
rake (0.8.1)
my problem / question: as soon as i have a named_scope declaration within the class, im experiencing the error above ( NoMethodError)
what am i doing wrong/missing?
thx tom
never , i figures it out, its working now on the console.as a newbie i forgot to chnage envrinment.rb
thx
hi ryan, great job as usual. quick question for you... when you are working around the inability to pass in a default value for the lamdba, you splat args and then in the proc you use "args.first || 2.weeks.ago"...
my question is why? why not just leave the argument to the lambda as "t" and then in the proc use "t || 2.weeks.ago" ?
seems to work for me, but i'm wondering if you had some other rationale for using the splat/first method.
You can also pass in a params hash into the named scope and use for example params[:newer_than] || 2.weeks.ago for the default parameter. I prefer this solution over using args when having more than one parameter as the named scope call becomes easier to understand and parameter order doesn't matter.
hey Ryan, still working on that image trouble. I'll let you know when i get it sorted out.
had a tricky scope question though.
Is it possible to order a find call my an associated model's attribute.
e.g.
@category.members.order_by_members_businesses_names do |member|
i would just do
@category.businesses.order_by_name do |business|
but i the member model has addtional info i need to access.
named_scope :order_by_members_businesses_names ...?
So would this replace the need to use the scope out plugin?
@chris ("why not just leave the argument to the lambda as "t" and then in the proc use "t || 2.weeks.ago" ?"):
It's so you can leave the arg out -- it fakes an optional parameter.
See:
http://pastie.org/258863
This is so easy, it's great! Thanks for the walkthrough.
Hi,
You didn't try the case where 2 named_scope have a block of code. In your screencast only :recent ha a block of code, but what about chaining it with :cheap that also had a block of code?
I can't make it work.
If anyone's interested, I've created a plugin which automagically generates common named scopes for all AR models and columns. See http://github.com/zubin/autoscope
>> Page
=> Page(id: integer, name: string, content: text, enabled: boolean, category_id: integer, created_at: datetime, updated_at: datetime)
...creates these (for example):
>> Page.enabled
>> Page.not_enabled
>> Page.named "foo"
>> Page.content_contains "bar"
>> Page.created_before 1.year.ago
Install:
./script/plugin install git://github.com/zubin/autoscope.git
Feedback welcome!
Thank you very much for this screencast.
Your tip about working with optional arguments saved my live :-)
Really great screencast again Ryan
Thanks again for this great episode!
Just what I have been looking for!
Hi Ryan,
Thanks so much for all your work. Question - I'm seeing that
scope = Product.scoped({})
is indeed doing a find which is undesired in my case. I'm on Rails 2.1. I'd like to build up the scopes before executing. Do I need to use your scope_builder?
Thanks!
Per comment #44 by me:
I was in the console when testing this. Appears to be ok when running in Rails.
Sorry for any confusion. Still seems a bit weird though.
In fact, I'm using your episodes instead of Rails API!
Thanks for sharing!
Hi,
I have been trying to get the named scopes to work in my model without any success.
I have model called Contact.
And my named scope is like the follow:-
class Contact < ActiveRecord::Base
named_scope :guardians, {:conditions =>["relationship = ?", 'Guardian']}
And in the IRB:-
>contact = Contact.find(2)
>contact.guardians
gives the following error:
NoMethodError: undefined method `gaudians' for #<Contact:0x636d4e4>
from F:/Projects/WorkshopProjects/testproject/vendor/rails/activerecord/lib/active_record/attribute_method
s.rb:256:in `method_missing'
from (irb):7
I am not sure what is going wrong.
I even tried using lambda as follows, but same thing happens:-
named_scope :test_guardians, lambda { { :conditions => ['relationship = ?', 'Guardian'] } }
Can you help me out here ?
Thanks in advance.
-Arjun
I have been trying to get the named scopes to work in my model without any success. How to do next?
Ryan, or any other expert,
I'm such a fan of named scopes now and see so many opportunities to streamline code. I ran into a problem. How can I create a named scope that uses an association and a lambda expression for parameterization. For example how would you retrieve all the products that are from a certain category type, for example 'electronics'. When I try something analagous to this I get an error: wrong number of arguments (3 for2).
named_scope :retrieve_by_type, :include=>:category, lambda{|type_id| {:conditions=>['categories.type'=type_id]}
-Thanks in advance
Hi,
i'm new, how can i see generated queries like you do in the bottom terminal ?
Since this was not clearly stated in text form I thought I would include this little snippet from the documentation on named_scope:
-----------------------
Unlike Shirt.find(...), however, the object returned by Shirt.red is not an Array; it resembles the association object constructed by a has_many declaration. For instance, you can invoke Shirt.red.find(:first), Shirt.red.count, Shirt.red.find(:all, :conditions => {:size => ‘small’}). Also, just as with the association objects, named \scopes act like an Array, implementing Enumerable; Shirt.red.each(&block), Shirt.red.first, and Shirt.red.inject(memo, &block) all behave as if Shirt.red really was an Array.
I am always been a great fan of your railscast. Keep up the good work.
Could it be possible to have a named_scope that include columns from another association?
something like:
named_scope :my_fields,:select => "..."
or maybe another way?
Thanks
If they are linking from the comment, I would consider it spam. If they post a comment, and it is on topic, be glad someone saw your blog and thought enough to post to it. http://www.hayda.net/
Thanks for a great post! My attention tends to drift after about 7 minutes, so i'll have to come back and watch again a couple times :)
Thanks for a great post! My attention tends to drift after about 7 minutes, so i'll have to come back and watch again a couple times :)
Hi I found your site from google. I enjoy your site too. thanks
to friends here agree. They all say the right things likewise.
I've been using has_finder for a while now, and it's been a real pleasure to work with. I'm really glad it's made it into rails
Thanks for the review, but I think many people are wanting this feature.
Thanks for these Rails 2.1 tips. Thanks for these Rails 2.1 tips.
thanks for this nice easy solution to a prob i was having in my DB. this will save a pile of time for me... thx
I will definitely try these types of rail casts . Thanks for sharing information about these.
Yip, I agree, another great screencast.
Quick questions, does this make all self.finds obsolete? Will the new conventions be named_scope instead of def self.find_some_criteria?
The new GHD.IV Styler is one of many hair care products in the most popular, which makes it easy for colleges and universities has straight hair. So you can always have a beautiful flowing straight hair, the use of imported PTC heating elements, fast heat up 180 degrees temperature does not harm hair, smooth ceramicsurface, straightened hair
The trick, if there is such a thing, is variety. Make certain you collect a steady and varied collection of links
The trick, if there is such a thing, is variety. Make certain you collect a steady and varied collection of links
thanks so much.
<a title="indir" href="http://www.fullpaylas.net/">indir</a>
Thanks.. for a great share
<a href="http://www.ebutube.org" title="Ebutube" rel="nofollow">Free Porn Videos - Ücretsiz Video İzle</a>
Nobel Peace Prize winner, President Obama, the country will be implemented in the next 10 years, announces new nuclear weapons strategy. Accordingly, the U.S., even in the event of biological or chemical attack will not apply to the atomic bomb. However, exceptions of Iran and North Korea.
Thanks a lot for the valuable information provided. We will look forward in more content like this.
http://www.hitbedava.com
http://www.agrisevdasi.com
http://www.onlynpara.tr.gg
http://www.paradelisi.tr.gg
Your post is quite informative and to the point. I have bookmarked to check out new stuff you post.
very very good post. <a href="http://www.super-e-world.com/LCD-Monitor/7-inch.php">7 inch LCD Monitor</a>
This was a really great Railscast highlighting probably my favorite new feature of Rails. Thanks.
Thanks for the useful code :) Its well hacked together for what I'm looking for :D
I might port it to python later :P
Oh, so unexpected, so surprise! Very touching, so well written and I have some perception, learning very much. Thank you for sharing. Very happy to see these and believe that your point of view. This same time,
Perfect suggestion! thank you to share your knowledge with us.
I have been trying to get the named scopes to work in my model without any success.
Thanks for this great resource page! May I request that you attempt to place freelance blogs according to geographic origins, so people w
I think you are referring to "black snake moan"..It stars Samuel L. Jackson and Christ
It's great to see an article like this. www.mbtselling.com www.jerseystown.com www.dunkjordan.com www.airmaxsupplier.com www.ghdbeautyhair.com
I was surfing net and fortunately came across this site and found very interesting stuff here. Its really fun to read. I enjoyed a lot. Thanks for sharing this wonderful information.
I was surfing net and fortunately came across this site and found very interesting stuff here. Its really fun to read. I enjoyed a lot. Thanks for sharing this wonderful information.
I was surfing net and fortunately came across this site and found very interesting stuff here. Its really fun to read. I enjoyed a lot. Thanks for sharing this wonderful information.
Thanks a lot! I am just learning Information of Subject.
Php and this was very easy to follow and helped a lot.
You really took time to explain every little bit.
Thanks again
Thank you for inquiring. Been working very nice.
An excellent blog about perfect.
Thank's again.
I have been using edge rails as of late just for this amazing functionality. In addition to the new level of dynamism this feature adds to Rails, it also adds such simplicity.
I am always been a great fan of your railscast. Keep up the good works.
"railscasts"- Great article and thanks for posting.You are one of my favourite site.You will always be in my bookmarks.
"railscasts"- Great article and thanks for posting.You are one of my favourite site.You will always be in my bookmarks.
<a href="http://www.ghd-straightener.com.au" target="_blank">ghd straightener</a>
<a href="http://www.ghd-straightener.com.au" target="_blank">ghd hair straightener</a>
Cool. Talking about makes sense. Is recognized. Hope that it will often share such a good text. I will
i'm new, how can i see generated queries like you do in the bottom terminal ?
I have been trying to get the named scopes to work in my model without any success. How to do next?
hey Ryan, still working on that image trouble. I'll let you know when i get it sorted out.
Hello, I arrived at this website by accident when I was exploring on Google then I came upon your web site. I have to tell you that your site is very interesting I really like your theme!
I am always been a great fan of your railscast. Keep up the good works.
As you have a blog about sharing was very nice.
I host it and different places in search of an active subject.
I thank you for information
Thanks a lot! I am just learning Information of Subject.
Php and this was very easy to follow and helped a lot.
You really took time to explain every little bit.
Thanks again...
Very interesting screencast ! Cool new feature ! Thanks for this tip !
a very successful site. Also very revealing article. Thanks to the contributors.
HOLA ME GUSTA MUCHO BRASIL POR QE HOY CHICOS LINDOS BYE MARIANA DE PERU
this definitely lowers the need to create custom "self.find..." methods. However I would hesitate getting too fancy here. If you have a complicated search which needs to dynamically build up the conditions based on the passed parameters, then I wouldn't use named_scope for this.
These kind of post are always inspiring and I prefer to read quality content so I happy to find many good point here in the post, writing is simply great, thank you for the post
This is something I was completely unaware of. Thanks for posting.
If they post a comment, and it is on topic, be glad someone saw your blog and thought enough to post to it
I wonder if these named scopes can be used in conjunction within existing search plugins like acts_as_ferret in one line
First time i have visited this site.I have read this blog,i noted that there are mostly good points which increase the importance of the blog.Nice work.Keep it up to write more good information.
<a href="http://www.wikipeers.com/legal/4327-Recover-Yourselves-by-Seeking-Mesothelioma-Attorneys-Texas.html">Mesothelioma Attorneys Texas</a>
Hi, where did you get this information can you please support this with some proof or you may say some good reference as I and others will really appreciate. This information is really good and I will say will always be helpful if we try it risk free. So if you can back it up. That will really help us all. And this might bring some good repute to you.
This article is very interesting. Thank you very much for sharing .
These kind of post are always inspiring and I prefer to read quality content so I happy to find many good point here in the post, writing is simply great, thank you for the post
The contents of the article drew my attention and so I wanted to thank you for enlighten me with an expression so well. I wish you continued sharing. Good work
Hi Admin, Me & my fellow classmates use your blogs as our reference materials. We look out for more interesting posts from your end about the same topic . Even the future updates about this topic would be of great help.
Published because of the useful information I know, thank you. You have given us your valuable comments I wish you continued thanks to informative content
Great!This article is creative,there are a lot of new idea,it gives me inspiration.I think I will also inspired by you and think about more new ideas
Great information. Interesting new features. Thanks for posting.
Thanks for sharing the information.It is definitely going to help me some time.
This is my first time i visit here. I found so many entertaining stuff in your blog, especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! Keep up the excellent work.
This is my first time i visit here. I found so many entertaining stuff in your blog, especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! Keep up the excellent work.
Thanks for a great post! My attention tends to drift after about 7 minutes, so i'll have to come back and watch again a couple times
Great job here guys. Keep the great posts coming. I follow your blog and I follow you on Twitter.
So many of us know how cool named_scopes are in ActiveRecord; they really make building complex queries quite pleasant compared to writing out big hairy SQL strings all over the place. However, in the past week as I have refactored my whole web application to use this excellent feature, I've run into some little-discussed items that I feel should be shared somewhere. Hope you enjoy, and anybody who is more advanced in their Rails-fu is welcome to give me some schooling as to where I've gone wrong on any of these, as most of my discoveries here are through trial and error.
<a href="http://www.kicksbar.com" title="air jordans shoes">air jordans shoes</a>
lots of good advice on your post. and as a return,i will buy a Cheap nfl jerseys to you for my thanking.
<a href=http://www.kicksbar.com>cheap jordan sneakers</a>Welcome to visit our company website:http://www.kicksbar.com
<a href=http://www.kicksbar.com>air jordans sneakers</a>Adrian: “Our response was: With PHP people learn that because they want to get jobs. With Java they learn that because they take computer science courses. With Python you learn it because you love it. Because you want to experience the beauty. I’m sure it’s the same way for Ruby. If I’m hiring a Python programmer the chances are that the person is good.”
David: “I totally agree.”
<a href=http://www.kicksbar.com>jordan air force one</a>
michael jordan sneakers
new jordan sneakers
cheap jordan sneakers
air jordans sneakers
Thanks for a great post! My attention tends to drift after about 7 minutes, so i'll have to come back and watch again a couple times
<a href=http://www.kicksbar.com>jordan air force 1</a>jordan air force 1
cheap jordan sneakers
air jordans sneakers
<a href=http://www.kicksbar.com>new jordan sneakers</a>new jordan sneakers
I had no idea this was the way it was. I have now changed my views.
You need to naturally increase your metabolism to succeed at dieting
How you can lower cholesterol without the side effects of harsh medications
I am always been a great fan of your railscast. Keep up the good works.
Hi, this is very nice posting, I enjoyed it a lot and I think other readers might enjoy it as well.
çok başarılı paylaşıma açık bir site olmuş başarılarınızın ve çalışmalarınızın devamını dilerim elinize emeginize saglık
I am always been a great fan of your railscast. Keep up the good works.
Merhabalar Web siteniz çok güzel. Zaten ziyaretçi defterinizdeki mesajlarda bunu gösteriyor. Emeği geçen herkesin ellerine sağlık
What Can be Done to Lower Cholesterol Without the Use of Medication and the Side Effects They Produce?
Thanks For Your Job. Its Very Interesting for me. Im eforush from kasablanka
The information you provided was very useful. Because of your help, thank you
The information you provided was very useful. Because of your help, thank you
LeBron's story is always associated with progress and victory.
The information you provided was very useful. Because of your help, thank you
The information you provided was very useful. Because of your help, thank you
Wow, this looks really cool. I especially like the efficiency of the generated SQL query when you concatenated the named_scopes. Really amazing and definately a great feature for bigger, data-filled applications.
Thanks for another great screencast!
The information you provided was very useful. Because of your help, thank you
Interesting article, Funny comment. Keep it up! I was just wondering how to pull this off. I'm guessing some clients probably want a mobile version for their site and so I'm glad you posted this because I was clueless earlier.
Throughout the 20th century, 80s, beyond the Jordan of basketball, it began to [url=http://www.discount-kicks.com/]cheap jordans[/url] and budding hip-hop icon. Basketball star and a common cultural features street musicians: rebellious, full of contempt between mainstream culture and provocative behavior.
[url=http://www.discount-kicks.com/]air max 2009[/url] was released in 2009, it sold well because all of the Nike brand in the world. The improved max air cushion technology was first used him in the [url=http://www.discount-kicks.com/]air max 2010[/url] air cushion crystal clear that the whole sole of the foot so this shoe is more visual impact.
sitenizi çok iyi buldum bilgiler her zaman işimize yarayabiliyor ama okumayı sevdiğimiz için daha fazla yazı koyasanız sevinirim.iyi çalışmalar.
Çok başarılı bir site olmuş. Elinize , emeğinize saglık. Başarılarınızın devamını dilerim.
Thanks for sharing your article. I really enjoyed it. I put a link to my site to here so other people can read it. My readers have about the same interets
Discount Wholesale Electronics, Wholesale Cell Phones, Electronic Gadgets and More from the Best Dropship Wholesaler
s.a. yeni siteniz hayırlı olsun. inşallah güzel çalışmaların yapılacağı ve duyurulacağı bir buluşma noktası olur temennisiyle..
Really trustworthy blog. Please keep updating with great posts like this one. I have booked marked your site and am about to email it to a few friends of mine that I know would enjoy reading..
You shouldn't really have finds anyway as soon as you're using find conditions.
bu sıtenın kurulmasında emegı gecen tum yetkılılere sonsuz tesekkur ederım...
Good article! Thank you so much for sharing this post.Your views truly open my mind.






