Ruby for Rails phần 9

Ruby Application Archive đóng vai trò là một kho lưu trữ đủ loại ứng dụng và các thư viện viết bằng Ruby, với hàng ngàn mục. Mặc dù số lượng ứng dụng sẵn có không lớn bằng với cộng đồng của Perl hay Python, vẫn có đủ loại công cụ và tiện ích nhằm hỗ trợ cho việc phát triển ngôn ngữ trong tương lai. | Soft programmatic extension of models 397 these particular models and they ve been chosen to represent a broad range of possibilities. But the point is that the door is open. As you become increasingly familiar and comfortable with Ruby writing this kind of code will come more and more naturally. We ll work model by model adding soft programmatic enhancement to the Work Customer and Composer models. To give you an overview here s a list of all the questions that we ll ask and answer by writing methods in this section The Work model Which publishers have published editions of this work What country is this work from What key is this work in The Customer model Which customers have ordered this work What open orders does this customer have What editions does this customer have on order What editions has this customer ever ordered What works does this customer have on order What works has this customer ever ordered The Composer model What editions of this composer s works exist What publishers have editions of this composer s works As you ll see all of these methods involve giving the models a boost a way to unearth and collect existing information that isn t available already in the form we want it through an existing method. NOTE PLAIN ARRAYS VS. MAGIC COLLECTIONS You should keep one important point in mind as you look at and eventually write soft enhancements to your models. When you gather together say an array of Edition objects by traversing a collection of Work objects and accumulating their editions you end up with a plain Ruby array. You don t end up with a magic ActiveRecord collection. You ll recall that in discussing arrays in chapter 11 we looked at ActiveRecord collections as an example of something array-like that is also endowed with methods and behaviors that go beyond those of the normal array. Those extra powers aren t added to arrays that you create even if they contain ActiveRecord objects. 398 CHAPTER 15 Programmatically enhancing ActiveRecord .

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.