Rails 中的自连接查询。未定义的方法错误
Self join query in Rails. Undefined method error
我正在 Rails 4 中创建一个巴士时刻表应用程序,我有一个时间表 Class,其中每条记录都有一个巴士站并且属于一个巴士服务外键。
我想显示每条巴士服务的起点站和终点站。我在时间表 class (bus_service) 上自我加入到 link 属于特定巴士服务的始发站和目的地站的时间表条目。
型号。 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' 错误。
时间表中没有方法错误#index
在视图中如何调用姐妹站名?或者我做错了什么。
谢谢
<% @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 %>
为了我的理智,
#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
I want to display the origin and destination stops served by each bus service
那么你将能够做到:
@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
这仍然是粗略的,但应该可以。
我没有包括 "destination" / "origin" 公交车站,因为它们没有在模型中定义。我想包含一个 Route
模型,但我无法根据示例数据使其工作。
希望对您有所帮助!
我正在 Rails 4 中创建一个巴士时刻表应用程序,我有一个时间表 Class,其中每条记录都有一个巴士站并且属于一个巴士服务外键。
我想显示每条巴士服务的起点站和终点站。我在时间表 class (bus_service) 上自我加入到 link 属于特定巴士服务的始发站和目的地站的时间表条目。
型号。 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' 错误。 时间表中没有方法错误#index
在视图中如何调用姐妹站名?或者我做错了什么。
谢谢
<% @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 %>
为了我的理智,
#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
I want to display the origin and destination stops served by each bus service
那么你将能够做到:
@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
这仍然是粗略的,但应该可以。
我没有包括 "destination" / "origin" 公交车站,因为它们没有在模型中定义。我想包含一个 Route
模型,但我无法根据示例数据使其工作。
希望对您有所帮助!