我是RoR的初學者。在移動到Rails中的下一個對象之前,如何循環對象和它的嵌套對象?
我試圖做到的是可循環的service_types
列表(剎車鼓,墊,等...)每service
類(剎車)纔去到屬於每個高科技接下來service
類別(懸浮液)。
我有以下的設置:
寶石
devise: user signup
cocoon: customizable nested objects
我認爲從我的服務/ 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>
服務.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
技術。RB模型(分貝與科技股的列表 - 經由devise
寶石註冊)
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
編輯 - 5/25 [原因:複製粘貼錯誤/錯字]
服務(包含具有服務主要類別列表的數據)
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
分貝記錄
服務類型(從繭中的寶石,讓我創建嵌套的業務類型與幾個自定義屬性)在services
分貝
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
引用記錄
經過幾天的研究,最接近我來完成我想要的是這個fr OM我的觀點:以上
<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
類別(暫停)騎自行車。
任何幫助,將不勝感激。 感謝
是什麼'服務'?你可以發佈它的定義嗎? – daslicious
我的歉意,我添加了服務模式。 –