2015-10-20 51 views
1

我正在Rails 4中創建一個公交時間表應用程序,並且我有一個時間表類,每個記錄都有一個公交站,屬於一個公交車服務外鍵。在Rails中自加入查詢。未定義的方法錯誤

我想顯示每個巴士服務提供的原點和目的地點。我在時間表類(bus_service)上進行自加入,以便將屬於特定總線服務的起點和終點站的時間表條目鏈接起來。

型號。 timetable.rb

has_many :timetable_sisters, class_name: "Timetable", primary_key: "bus_service_id", foreign_key: "bus_service_id" 
belongs_to :timetable_sister_b, class_name: "Timetable", foreign_key: "bus_service_id" 
belongs_to :bus_stop 
belongs_to :bus_service 

def self.joined(from_stop_id, to_stop_id) 
joins(:timetable_sisters). 
where("timetables.bus_stop_id = ? AND timetable_sisters_timetables.bus_stop_id = ?", from_stop_id, to_stop_id) 
end 

控制器:timetable_controller.rb

@timetables = Timetable.joined(params[:from_stop_id], params[:to_stop_id]) 

查看:index.html.erb

<% @timetables.each do |timetable| %> 
<tr> 
<td><%= timetable.bus_service_id %></td> 
<td><%= timetable.bus_stop.name %></td> 
<td><%= timetable.timetable_sister.bus_stop.name %></td> 
<td><%= timetable.departure_time %></td> 
</td></tr> 
<% end %> 

生成的SQL看起來不錯:

SELECT "timetables".* 
FROM "timetables" 
INNER JOIN "timetable_sisters" "timetable_sisters_timetables" 
ON "timetable_sisters_timetables"."bus_service_id" = "timetables"."bus_service_id" 
WHERE (timetables.bus_stop_id = '25' 
AND timetable_sisters_timetables.bus_stop_id = '27') 

但我得到一個未定義的方法'timetable_sister'錯誤的視圖文件。 時間表中的NoMethodError#索引

我應該如何在視圖中呼叫姐妹電臺名稱?或者我正在做其他事情。

感謝

+2

您對「timetable_sisters」有'to many'關係。因此你不能調用.timetable_sister。相反,你需要遍歷結果,例如'timetable.timetable_sisters.each' –

+0

這就是偉大的davidsatch。有用。 :)但邏輯上它給了兩個姐姐時間表停止結果。如何將結果限制在列中未列出的結果之前? – Enrique

回答

1
<% @timetables.each do |timetable| %> 
<tr> 
<td><%= timetable.bus_service_id %></td> 
<td><%= timetable.bus_stop.name %></td> 
<td><% timetable.timetable_sisters.each do |t_sis| %> 
    <%= t_sis.bus_stop.name %>, 
<% end %></td> 
<td><%= timetable.departure_time %></td> 
</td></tr> 
<% end %> 
+0

這是偉大的davidsatch。有用。 :)但邏輯上它給了兩個姐姐時間表停止結果。如何將結果限制在列中未列出的結果之前? – Enrique

+0

您需要使用巴士服務來識別每條路線,並只顯示一次。例如,您可能需要瀏覽每個bus_service並顯示該服務中的每個站點,而不是列出時間表。 –

0

對於我的理智的緣故,

#app/models/timetable.rb 
class Timetable < ActiveRecord::Base 
    #columns id | service_id | bus_stop_id | arrival_time | created_at | updated_at 
    belongs_to :bus_stop 
    belongs_to :service 
end 

#app/models/service.rb 
class Service < ActiveRecord::Base 
    #columns id | name | number | etc | created_at | updated_at 
    has_many :timetables 
    has_many :bus_stops, through: :timetables 
end 

#app/models/bus_stop.rb 
class BusStop < ActiveRecord::Base 
    #column id | name | geox | geoy | created_at | updated_at 
    has_many :timetables 
    has_many :services, through: :timetables 
end 

我想要顯示的出發地和目的地停止各巴士服務

然後你會成爲ab樂做:

@services = Service.all 
@services.each do |service| 
    # service.number -> "56" 
    service.bus_stops.each do |stop| 
     stop.name #-> " 
    end 
end 

如果你想繪製一個特定的路線,你可以做到以下幾點:

@service = Service.find "1" 
@services = Timetable.where service: @service 

@services.each do |route| 
    route.bus_stops.each do |stop| 
     stop.name 
     stop.geox 
     stop.geoy 
    end 
end 

這仍然是粗略的,但它應該工作。

我沒有包含「目的地」/「原點」巴士站,因爲它們沒有在模型中定義。我想包括一個Route模型,但我無法使用示例數據工作。

希望這會有所幫助!

相關問題