如何在移动到 Rails 中的下一个对象之前循环访问一个对象及其嵌套对象?
How do I cycle through an object and it's nested objects before moving on to the next object in Rails?
我是 RoR 的初学者。
我想要完成的是循环浏览 service_types
(刹车、鼓、刹车片等...) 每个 [=20] =] 类别 (刹车) 属于每个技术,然后再进入下一个 service
类别 (悬架).
我的设置如下:
宝石
devise: user signup
cocoon: customizable nested objects
我的观点 services/show.html.erb
<tbody>
<% @services.each do |service| %>
<tr>
<td><%= service.name %></td>
<td><%= **??? Don't know how to cycle through to capture the tech's list `services` and `service types**` %>
</tr>
<% end %>
</tbody>
service.rb 型号
class Service < ActiveRecord::Base
has_many :service_types
belongs_to :tech
accepts_nested_attributes_for :service_types, :reject_if => :all_blank, :allow_destroy => true
end
techs_controller.rb
class TechsController < ApplicationController
def index
@techs = Tech.all
@services = Service.all
end
def show
@tech = Tech.find(params[:id])
@services = @tech.services.all
#@service_types = @service.service_types.all
end
end
tech.rb 模型(带有技术列表的数据库 - 通过 devise
gem 注册)
class Tech < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :service_types
has_many :services
has_many :appointments
has_many :customers, :through => :appointments
end
Edited - 5/25 [Reason: copy paste error / typo]
服务(包含具有服务主要类别列表的数据)
mysql> describe services;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
| tech_id | int(11) | YES | | NULL | |
+------------+--------------+------+-----+---------+----------------+
tech_id
引用了 tech
数据库中的一条记录
ServiceType(来自 cocoon gem,它允许我创建具有多个自定义属性的嵌套服务类型)
mysql> describe service_types;
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| description | varchar(255) | YES | | NULL | |
| time | varchar(255) | YES | | NULL | |
| price | decimal(8,2) | YES | | NULL | |
| decimal | decimal(8,2) | YES | | NULL | |
| service_id | int(11) | YES | MUL | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
service_id
引用了 services
数据库中的一条记录
经过几天的研究,我认为最接近完成我想要的是:
<tbody>
<% @services.each do |service| %>
<tr>
<td><%= service.name %></td>
<td><%= service.service_types.all.collect{|s| [s.name, s.time, s.price} %></td>
</tr>
<% end %>
</tbody>
</table>
上面的结果是这样的:
Brakes [["Rotors", 1HR], ["Drums", 1HR]]
我要查找的输出如下:
下面是我正在寻找的输出:
+---------+----------+-----+-------+
| Brakes | Type | Time | Price |
+---------+--------+-------+-------+
| Rotors | 1HR | 100 |
| Drums | 1HR | 100 |
+-------------+----------+-----+-------+
| Suspension | Type | Time | Price |
+-------------+--------+-------+-------+
| Struts | 2HR | 100 |
| Shocks | 1HR | 100 |
我想循环通过 service
(刹车) 并且所有这些都是嵌套的 service_types
,然后再继续循环通过下一个 service
类别(暂停)。
如有任何帮助,我们将不胜感激。
谢谢
<% @services.each do |service| %>
<tr>
<td><%= service.name %></td>
<td>
<table>
<tr>
<th>Name</th>
<th>Time</th>
<th>Price</th>
</tr>
<% service.service_types.each do |service_type| %>
<tr>
<td><%= service_type.name %></td>
<td><%= service_type.time %></td>
<td><%= service_type.price %></td>
</tr>
<%end%>
</table>
</td>
</tr>
<% end %>
还有一件事
service_type_id
服务 table 中不需要。
我是 RoR 的初学者。
我想要完成的是循环浏览 service_types
(刹车、鼓、刹车片等...) 每个 [=20] =] 类别 (刹车) 属于每个技术,然后再进入下一个 service
类别 (悬架).
我的设置如下:
宝石
devise: user signup
cocoon: customizable nested objects
我的观点 services/show.html.erb
<tbody>
<% @services.each do |service| %>
<tr>
<td><%= service.name %></td>
<td><%= **??? Don't know how to cycle through to capture the tech's list `services` and `service types**` %>
</tr>
<% end %>
</tbody>
service.rb 型号
class Service < ActiveRecord::Base
has_many :service_types
belongs_to :tech
accepts_nested_attributes_for :service_types, :reject_if => :all_blank, :allow_destroy => true
end
techs_controller.rb
class TechsController < ApplicationController
def index
@techs = Tech.all
@services = Service.all
end
def show
@tech = Tech.find(params[:id])
@services = @tech.services.all
#@service_types = @service.service_types.all
end
end
tech.rb 模型(带有技术列表的数据库 - 通过 devise
gem 注册)
class Tech < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :service_types
has_many :services
has_many :appointments
has_many :customers, :through => :appointments
end
Edited - 5/25 [Reason: copy paste error / typo]
服务(包含具有服务主要类别列表的数据)
mysql> describe services;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
| tech_id | int(11) | YES | | NULL | |
+------------+--------------+------+-----+---------+----------------+
tech_id
引用了 tech
数据库中的一条记录
ServiceType(来自 cocoon gem,它允许我创建具有多个自定义属性的嵌套服务类型)
mysql> describe service_types;
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| description | varchar(255) | YES | | NULL | |
| time | varchar(255) | YES | | NULL | |
| price | decimal(8,2) | YES | | NULL | |
| decimal | decimal(8,2) | YES | | NULL | |
| service_id | int(11) | YES | MUL | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
service_id
引用了 services
数据库中的一条记录
经过几天的研究,我认为最接近完成我想要的是:
<tbody>
<% @services.each do |service| %>
<tr>
<td><%= service.name %></td>
<td><%= service.service_types.all.collect{|s| [s.name, s.time, s.price} %></td>
</tr>
<% end %>
</tbody>
</table>
上面的结果是这样的:
Brakes [["Rotors", 1HR], ["Drums", 1HR]]
我要查找的输出如下:
下面是我正在寻找的输出:
+---------+----------+-----+-------+
| Brakes | Type | Time | Price |
+---------+--------+-------+-------+
| Rotors | 1HR | 100 |
| Drums | 1HR | 100 |
+-------------+----------+-----+-------+
| Suspension | Type | Time | Price |
+-------------+--------+-------+-------+
| Struts | 2HR | 100 |
| Shocks | 1HR | 100 |
我想循环通过 service
(刹车) 并且所有这些都是嵌套的 service_types
,然后再继续循环通过下一个 service
类别(暂停)。
如有任何帮助,我们将不胜感激。 谢谢
<% @services.each do |service| %>
<tr>
<td><%= service.name %></td>
<td>
<table>
<tr>
<th>Name</th>
<th>Time</th>
<th>Price</th>
</tr>
<% service.service_types.each do |service_type| %>
<tr>
<td><%= service_type.name %></td>
<td><%= service_type.time %></td>
<td><%= service_type.price %></td>
</tr>
<%end%>
</table>
</td>
</tr>
<% end %>
还有一件事
service_type_id
服务 table 中不需要。