
- Learning Ruby on Rails
- Rails 2.1 Home
- Rails 2.1 Introduction
- Rails 2.1 Installation
- Rails 2.1 Framework
- Rails 2.1 Dir Structure
- Rails 2.1 Examples
- Rails 2.1 Database Setup
- Rails 2.1 Active Records
- Rails 2.1 Migrations
- Rails 2.1 Controllers
- Rails 2.1 Views
- Rails 2.1 Layouts
- Rails 2.1 Scaffolding
- Rails 2.1 and AJAX
- Rails 2.1 Uploads Files
- Rails 2.1 Sends Emails
- Advanced Ruby on Rails 2.1
- Rails 2.1 RMagick Guide
- Rails 2.1 Basic HTTP Auth
- Rails 2.1 Error Handling
- Rails 2.1 Routes System
- Rails 2.1 Unit Testing
- Advanced Ruby on Rails 2.1
- Rails 2.1 Tips & Tricks
- Quick Reference Guide
- Quick Reference Guide
- Ruby on Rails 2.1 Useful Resources
- Ruby on Rails 2.1 - Resources
- Ruby on Rails 2.1 - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Ruby on Rails 2.1 - Nested with-scope
The following example shows how we can nest with_scope to fetch different results based on requirements.
# SELECT * FROM employees # WHERE (salary > 10000) # LIMIT 10 # Will be written as Employee.with_scope( :find => { :conditions => "salary > 10000", :limit => 10 }) do Employee.find(:all) end
Now, check another example that shows how scope is cumulative.
# SELECT * FROM employees # WHERE ( salary > 10000 ) # AND ( name = 'Jamis' )) # LIMIT 10 # Will be written as Employee.with_scope( :find => { :conditions => "salary > 10000", :limit => 10 }) do Employee.find(:all) Employee.with_scope( :find => { :conditions => "name = 'Jamis'" }) do Employee.find(:all) end end
The following example shows how the previous scope is ignored.
# SELECT * FROM employees # WHERE (name = 'Jamis') # is written as Employee.with_scope( :find => { :conditions => "salary > 10000", :limit => 10 }) do Employee.find(:all) Employee.with_scope( :find => { :conditions => "name = 'Jamis'" }) do Employee.find(:all) end # all previous scope is ignored Employee.with_exclusive_scope( :find => { :conditions => "name = 'Jamis'" }) do Employee.find(:all) end end
rails-quick-guide.htm
Advertisements