Ruby Rails,如何为另一个 class 设置一组唯一的属性

Ruby on Rails, How to set a group of attributes unique for another class

抱歉,标题难以理解,但我不知道如何解释这个问题:D 我的项目是建设一个轨道交通站点,所以我必须写一些路线、站点、火车等等。 我想知道如何单击路线并仅查看它自己的停靠点。 现在我一般只能看到所有停靠点。 我知道我很困惑,但我希望你能帮忙..谢谢

您有两个模型 RouteStop,您需要定义这些 类 之间的关联才能解决您的问题

class Route < ActiveRecord::Base
  has_many :stops
end

class Stop < ActiveRecord::Base
  belongs_to :route
end

所以 stops table 将有一个 route_id 列,要获取属于一条路线的所有停靠点,您可以这样做

route = Route.where(id: params[:route_id]).first
stops = route.stops

希望对您有所帮助!