Doubt about relationship between Rails tables

Asked

Viewed 554 times

1

Hello, I’m a beginner in Rails, (setting up my first system) and I have two questions about relationship between tables:

1.) I have a model Row with the attributes origin and fate which shall refer to another Local, i.e., Line has an origin attribute which is a location and another destination attribute which is another location, as I do this association?

2.) Line represents a path between one place and another. In addition to the points of origin and destination the line passes in several reference points when it is going (origin > destination) and many more when it is coming back (destination > origin). Each reference point of this is a "referenced" location, which in addition to the attributes inherited from Local must have a "distance" information, i.e., Line, Sense One, after 20 minutes passes in Place x.

Looking at the relationship between the objects (if facilitating understanding would have something like this):

  • Place {description:string}
  • Extends Reference Local { distance:integer }
  • Line {origin:Place, destination:Place, referencesIda:[Reference], referencesVolt:[References]}

More like creating the templates to represent this??bold text

1 answer

1

First you have a Line entity that has multiple Local entities, and if a Location belongs to only one Line you will have a Line that belongs to a Location.

According to the same entity Line has several entities Reference

class Linha
  has_many :locais
  has_many :referencia 
end

class Local
  belongs_to :linha
end

class Referencia
  belongs_to :linha
end

References can have attributes indicating the position next to the places, so you can calculate the value of the distance between the references and/or places.

Any doubt about Uides' relationships is an excellent reference: http://guides.rubyonrails.org/association_basics.html#the-has-Many-Association

Browser other questions tagged

You are not signed in. Login or sign up in order to post.