2015-05-25 14 views
0

我是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類別(暫停)騎自行車。

任何幫助,將不勝感激。 感謝

+0

是什麼'服務'?你可以發佈它的定義嗎? – daslicious

+0

我的歉意,我添加了服務模式。 –

回答

2
<% @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

在服務表不是必需的。

+0

非常感謝你@ pardeep-dhingra!我可以發誓我做了類似的事情,但收到錯誤。我將我的帖子更正爲您的'service_type_id'評論。 –

+0

@captainawesome是否已完成此解決方案? –

+0

@captainawesome標記它正確,如果這個解決方案幫助你:) –