TAILIEUCHUNG - Agile Web Development with Rails phần 5

Để xác định tên của các cột để kiểm tra, đăng nhập Ghi lại chỉ đơn giản là tách tên theo find_by_ hoặc find_all_by_ xung quanh _and_ chuỗi. Điều này là đủ tốt hầu hết thời gian, nhưng bị phá vỡ nếu bạn đã bao giờ có một tên cột như tax_and_shipping. Trong những trường hợp này, bạn sẽ phải sử dụng các phương pháp tìm thông thường. | CRUD Create Read Update Delete 211 To determine the names of the columns to check Active Record simply splits the name that follows the find_by_ or flnd_all_by_ around the string _and_. This is good enough most of the time but breaks down if you ever have a column name such as tax_and_shipping. In these cases you ll have to use conventional finder methods. And no there isn t a find_by_ form that lets you use _or_ rather than _and_ between column names. Reloading Data In an application where the database is potentially being accessed by multiple processes or by multiple applications there s always the possibility that a fetched model object has become stale someone may have written a more recent copy to the database. To some extent this issue is addressed by transactional support which we describe on page 237 . However there ll still be times where you need to refresh a model object manually. Active Record makes this easy simply call its reload method and the object s attributes will be refreshed from the database. stock RUBY loop do puts Price sleep 60 end In practice reload is rarely used outside the context of unit tests. Updating Existing Rows After such a long discussion of finder methods you ll be pleased to know that there s not much to say about updating records with Active Record. If you have an Active Record object perhaps representing a row from our orders table you can write it to the database by calling its save method. If this object had previously been read from the database this save will update the existing row otherwise the save will insert a new row. If an existing row is updated Active Record will use its primary key column to match it with the in-memory object. The attributes contained in the Active Record object determine the columns that will be updated a column will be updated in the database even if its value has not changed. In the following example all the values in the row for order 123 will

TỪ KHÓA LIÊN QUAN
Đã 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.