Ruby on Rails Modeling

0

I have a question that I can not see how to resolve.

I have to run a stock program in Ruby on Rails that contains some hardware, which will contain some insights.

For example, I will have computers, printers, phones, among others, each with its table, as it contains unique elements each.

So I would need to create a 1-to-N relationship in Ruby on Rails, because a computer might have N inquiries, but a printer might also contain N inquiries, the phone, and all other hardware.

So the question is, do I have to reference all the tables in the creation of the inquiry, but just fill in the one I would use? for example:

Rails g scaffold Inquiry description: string data: date computer: reference printer: reference telephone: reference

I do not think so, but I can not figure out which one would be right.

    
asked by anonymous 04.10.2018 / 17:55

2 answers

0

I would not create a table for each type of equipment, but an equipment table.

In this case, equipment has N checks normally.

    
09.10.2018 / 15:56
0

I think it's logical that Alex said. Each equipment has several inquiries, while several inquiries belong to an equipment. This is a one-to-many relationship (one_to_many).

Then more or less would be

Rails g scaffold Equipment name:string description:string

And then

Rails g scaffold Inquiry inquiry:text equipment:references

Once the rails db: migrate, just specify the relationship between these two models.

No equipment model has_many: inquiries

No model Inquiry belongs_to: equipment

Probably some adjustments will have to be made in the Views, since in Equipments it will be possible to see, and to edit the investigations. If you are doing multiple tests, remember that you can undo changes just by swapping the "g" from generate to "d" from destroy.

Ex: rails d scaffold Equipment

I know this sounds simple, but this can avoid a lot of problems. Especially when we're just testing.

    
14.01.2019 / 21:32