Ruby on rails 如何在对象和它之间循环';在移动到Rails中的下一个对象之前,是否删除嵌套对象?
我是RoR的初学者 我试图完成的是,在进入下一个Ruby on rails 如何在对象和它之间循环';在移动到Rails中的下一个对象之前,是否删除嵌套对象?,ruby-on-rails,ruby,devise,cocoon-gem,Ruby On Rails,Ruby,Devise,Cocoon Gem,我是RoR的初学者 我试图完成的是,在进入下一个维修类别(悬架)之前,根据属于每个技术的维修类别(制动器),循环浏览维修类型列表(制动器、制动鼓、制动片等) 我有以下设置: 宝石 设计:用户注册 cocoon:可定制的嵌套对象 我的服务中的我的视图/show.html.erb <tbody> <% @services.each do |service| %> <tr> <td><%= service.nam
维修
类别(悬架)之前,根据属于每个技术的维修
类别(制动器),循环浏览维修类型列表(制动器、制动鼓、制动片等)
我有以下设置:
宝石
设计:用户注册
cocoon:可定制的嵌套对象
我的服务中的我的视图/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>
已编辑-5/25[原因:复制粘贴错误/打字错误]
服务(包含包含服务主要类别列表的数据)
tech\u id
引用tech
db中的记录
ServiceType(来自cocoon gem,它允许我创建具有多个自定义属性的嵌套服务类型)
service\u id
引用services
db中的记录
经过几天的研究,从我的观点来看,我最接近实现我想要的是:
<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>
我想在进入下一个维修
类别(悬架)之前,先循环一次维修
(制动器)及其所有嵌套的维修类型
。
任何帮助都将不胜感激。
谢谢
名称
时间
价格
还有一件事
服务类型标识
服务表中不需要。是什么服务
?你能发布它的定义吗?很抱歉,我已经添加了服务模式。非常感谢@pardeep dhingra!我可以发誓我做了类似的事情,但收到了错误。我将我的帖子更正为您的服务类型\u id
评论。@captainawesome完成了吗?此解决方案有效吗?@captainawesome如果此解决方案对您有帮助,请将其标记为正确:)
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
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
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 | |
+------------+--------------+------+-----+---------+----------------+
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 | |
+-------------+--------------+------+-----+---------+----------------+
<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 | Type | Time | Price |
+---------+--------+-------+-------+
| Rotors | 1HR | 100 |
| Drums | 1HR | 100 |
+-------------+----------+-----+-------+
| Suspension | Type | Time | Price |
+-------------+--------+-------+-------+
| Struts | 2HR | 100 |
| Shocks | 1HR | 100 |
<% @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 %>