Delegation is a feature Rails introduced in it’s 2.2 version, and in my opinion are quite useful and somehow something we don’t see too much around. The concept of delegation is to take some methods and send them off to another object to be processed.
Let me explain this with a brief example:
Suppose you have a User class for anyone registered on your site, and a Customer class for those who have actually placed orders:
class User < ActiveRecord::Base
belongs_to :customer
end
class Customer < ActiveRecord::Base
has_one :user
end
As for now, if you are in a Customer instance, you can get their User information doing@customer.user.name
, or @customer.user.email
. Delegation allows you to simplify this:
class User < ActiveRecord::Base
belongs_to :customer
end
class Customer < ActiveRecord::Base
has_one :user
delegate :name, :name=, :email, :email=, :to => :user
end
Now you can refer to @customer.name
and @customer.email
to retrieve and set values for those attributes directly. Pretty nice, huh?
We are now working on some code to make possible to inherit behaviour, along with polymorphic associations, so when you create a Cutomer, the User gets created as well with the data you provided when creating the customer, and so on.
So keep posted, for there will be more to come…
link:http://blog.wyeworks.com/2009/6/4/rails-delegate-method
http://railsmagazine.com/articles/4
分享到:
相关推荐
Now in version 4, Rails is mature and powerful, and to use it effectively you need more than a few Google searches. You'll find no substitute for the guru's-eye-view of design, testing, deployment, ...
Rails 3 in Action 2011年9月新鲜出炉,针对最新的Rails 3.1进行说明
What’s New In Rails 2.1: A Well Produced, Free E-Book
Rails 4 in Action, Second Edition.pdf
8 Setting up a Todo application in Rails 8 Analyzing folder structure of a Rails application 10 Creating views 12 Styling views using CSS 14 Redesigning the Todo application 16 Challenges in styling ...
Rubyisms in Rails
Detection of critical defects in rails using ultrasonic surface wave
Scrum Poker in Rails5, docker-compose
WeChat in Rails 的 API、命令和消息处理
唔,1分应该还是有人下的吧,共同学习进步,Ruby on Rails is an open source web framework.... "Rails 4 in Action" is a fully-revised second edition of "Rails 3 in Action." This hands-on, compreh...
We still start with a step-by-step walkthrough of building a real application, and in-depth chapters look at the built-in Rails features. This edition now gives new Ruby and Rails users more ...
kanban, Kanban 看板 is a Trello clone in Rails and Backbone.js.zip
rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails ...
[Pragmatic Bookshelf] Crafting Rails Applications Expert Practices for Everyday Rails Development (E-Book) ☆ 图书概要:☆ Rails 3 is a huge step forward. You can now easily extend the framework, ...
The more confident reader might be able to get started in Rails using just this book, extensive online resources, and his wits, but there are other publications that are more introductory in nature ...
Bootstrap is a free, open source CSS and JS framework that helps you create websites in minutes, and Ruby on Rails, or Rails as it's known, is an open source web application framework written in the ...
Ruby on Rails Guides v2 - Ruby on Rails 4.2.5
《Ruby on Rails Tutorial》中文版(原书第2版,涵盖 Rails 4) Ruby 是一门很美的计算机语言,其设计原则就是“让编程人员快乐”。David Heinemeier Hansson 就是看重了这一点,才在开发 Rails 框架时选择了 Ruby...