简体   繁体   English

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

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

sorry for the unintelligible title but I didn't know how to explain the problem :D My project consists in building a rail transport site, so I have to write some routes, stops, trains and so on.. I want to know how I can click on the route and see just its own stops. 抱歉,标题难以理解,但我不知道如何解释问题:D我的项目包括建设铁路运输站点,因此我必须写一些路线,车站,火车等。.我想知道我如何可以点击该路线,然后仅查看其自身的站点。 Now I can see only all stops in general. 现在,我只能看到所有站点。 I know I'm very confusing, but I hope you can help..thanks 我知道我很困惑,但希望您能帮忙..谢谢

You have two models Route and Stop , you need to define associations between these classes to solve your problem 您有两个模型RouteStop ,需要定义这些类之间的关联以解决您的问题

class Route < ActiveRecord::Base
  has_many :stops
end

class Stop < ActiveRecord::Base
  belongs_to :route
end

So stops table will have a route_id column, to fetch all the stops that belong to a route, you can do this 因此, stops表将具有route_id列,以获取属于一条路线的所有站点,您可以执行此操作

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

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM