Home>

I am a beginner of the first question.
While developing an app with Ruby on Rails, I was worried because the association of the parts related to the four models was complicated.

To briefly explain the outline of the app we are developing, we can create and register the performance composition of the sport called gymnastics.

I will write it concisely by simplifying and rewriting the parts that are difficult to understand so that the parts that are difficult to convey are eliminated as much as possible.

Model description

I tried to summarize in Excel so that it is easy to understand how the four models are related.
Where they are the same color, it means that they are connected by an association.

There is a User model and a Technique model that contains information on techniques.
One user can register many tricks via the Add model. (One-to-many relationship)
One user can select 3 of the techniques registered via the Add model and create a performance composition with the Pommel model. (One-to-one relationship?)

Question ①

Please let me know if there are any mistakes in the association in the first place.

has_many: techniques, dependent:: destroy
has_many: adds, dependent:: destroy
has_many: added_techniques, through:: adds, source:: techniques
has_many: pommels, dependent:: destroy
has_many: my_pommels, through:: pommels, source:: technique
belongs_to: user
has_many: adds, dependent:: destroy
has_many: added_users, through:: adds, source:: user
has_many: pommels, through:: adds, source:: user
belongs_to: technique
belongs_to: user
validates_uniqueness_of: techniques_id, scope:: user_id
has_one: pommel, foreign_key:'first', class_name:'Pommel', dependent:: destroy
has_one: pommel, foreign_key:'second', class_name:'Pommel', dependent:: destroy
has_one: pommel, foreign_key:'third', class_name:'Pommel', dependent:: destroy
belongs_to: user
belongs_to: technique, foreign_key:'first', class_name:'Technique', optional: true
belongs_to: technique, foreign_key:'second', class_name:'Technique', optional: true
belongs_to: technique, foreign_key:'third', class_name:'Technique', optional: true
belongs_to: add, foreign_key:'first', class_name:'Add', optional: true
belongs_to: add, foreign_key:'second', class_name:'Add', optional: true
belongs_to: add, foreign_key:'third', class_name:'Add', optional: true
Question ②

The first, second, and third columns of the Pommel model match the id in the Technique model.

How should I describe if I want to retrieve the Technique model record with ids that match the first, second, and third columns of the Pommel model and display it in the name column?

The code below didn't work because I couldn't specify the corresponding column in the Pommel model.

Pommel.joins (: techniques) .select ("pommels. *, Techniques. *"). First.name
Thanks for your answer!
  • Answer # 1

    In the first place, the related definition is messed up.
    First, let's stop Add, the many-to-many intermediate table is typically a TechniqueUser with model names in alphabetical order. If the intermediate table has a special meaning, it's better to name it, but it's probably not Add.
    Although it is a user

    has_many: techniques, dependent:: destroy ⇐ This would be unique to the User.
    has_many: techniques, through :: techniques_users

    has_many: added_techniques ⇐ meaningless
    Even if you have pommel multiples, 1: there are many, so the intermediate table my_pommels is unnecessary.

    By the way, please review and rewrite other models as well.