1

I have a relationship between compositions and languages. One composition should be written in one and only one language.

My schema.rb contains the following lines:

...
create_table "compositions", force: :cascade do |t|
    ...
    t.integer  "product_language_id", null: false
end

...
add_foreign_key "compositions", "languages", column: "product_language_id"
...

I realized that the relationship was written wrong, so I changed the models to be like this:

  • previously there was a belongs_to :language line in composition.rb which I changed to has_one :language
  • previously there was a has_many :compositions line in language.rb which I changed to belongs_to :composition

Edit: FIRST QUESTION: is the procedure I made correct? I'm still a beginner at Ruby on Rails.

Now, in rails_admin, there's no possibility to select the language under the new form for composition, there is the line but no list, box nor anything, just the label name 'language', although I have an entry for it in its table.

Edit 2: Once reverting the relationship back to its initial status which I supposed it were incorrect, in rails_admin there is the possibility to add compositions from the language form, but I'd like to have also a drop down menu in the composition form to select the language, which is not appearing. Any suggestion?

Can you tell me where I'm failing? Thanks in advance

Simon
  • 701
  • 6
  • 25

1 Answers1

0

Thanks to this upwork freelancer I corrected it leaving the relationship in its original belongs_to / has_many status with the foreign key addition in composition.rb:

belongs_to :language, :foreign_key => 'product_language_id'

Rails assumes everything will be done according to convention. So foreign key of a table is expected to be tablename_id. When we break from the convention, we have to add additional options in our model to tell Rails that the foreign key is not what it expects, its something different. We could write it this way as well:

belongs_to :product_language, class_name: 'Language'
Simon
  • 701
  • 6
  • 25