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 쿼리를 제공합니다. 얼마나 많은 SQL 쿼리가 실행될 것입니까?
이 고객
SELECT COUNT(*) FROM orders where orders.cust_key= customers.cust_key;
- 하나의 SQL 쿼리를 발행하고 위의 루비 온 레일 조각과 동일 HTML을 생성하는 JSP 단편을 쓰기 FROM
SELECT 주소를 쿼리합니다. 당신이 변경하면 ...
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
다음은이 짧은 [레일 가이드의 시작 안내서]와 같이 덜 구체적인 것으로 시작하는 것이 현명합니다 (http://guides.rubyonrails.org/getting_started .html)를 사용하여 Rails를 신속하게 살펴볼 수 있습니다. –