0
我已經做了第一個,並且想檢查我是否正確或不正確?我也不知道如何去做數字2 Ruby ORM 考慮表格「customers」和「orders」上的以下兩個活動記錄定義。 orders表中有一個外鍵「cust_key」,它引用了「customers」的主鍵,該鍵也被命名爲「cust_key」。Ruby on rails轉換爲sql查詢
Table:
customers-
cust_key
address
orders-
order key
cust_key
order_priority
total_price
1 class Customer < ActiveRecord::Base
2 set_table_name "customers"
3 set_primary_key "cust_key"
4 has_many :orders, :foreign_key => "cust_key」
5 End
1 class Order < ActiveRecord::Base
2 set_table_name "orders"
3 belongs_to :customer, :foreign_key => "cust_key"
4 set_primary_key "order_key」
5 end
Consider the following piece of Ruby-on-Rails code.
1 <table>
2 <% Customers.all.each.do |c| %>
3 <tr>
4 <td><%= c.address %></td>
5 <td><%= c.orders.count() %></td>
6 </tr>
7 <% end %>
8 </table>
問題:
- 提供的SQL查詢,這將導致從執行這一塊的Ruby-on-Rails的的。總共有多少SQL查詢將被執行?
2查詢
客戶
SELECT COUNT(*) FROM orders where orders.cust_key= customers.cust_key;
- 寫發出只有一個SQL查詢,並創建了上述的Ruby-on-Rails的片段相同的HTML在JSP片斷選擇地址。
1)在'rails console'中,您可以在任何查詢中調用'.to_sql'來獲得生成的SQL查詢(例如:'Customer.where(last_purchase:1.week.ago..Time.zone.now ).to_sql'。)2)在'log/development.log'文件中,您應該顯示所有已執行的查詢。你可以使用'tail -f log/development.log'在你的控制檯中定尾這個文件。 –
即時新聞紅寶石,甚至不知道如何運行 – keivn
然後,明智的做法是從一些不太具體的東西開始,比如這個簡短的[Rails指南中的入門指南](http://guides.rubyonrails.org/getting_started .html),以便快速瀏覽Rails。 –