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。 –