I have problem in using Associations in Ruby on Rails

Viewed 53

I'm doing a project with Ruby connected with the database in Postgresql. I have three tables that consist of numbers and one table consists of numbers referred to words in the other tables, so I need to show the words instead of the numbers in Ruby. I tried has_many and belongs_to, as in the photos but I got always the last number in the column called path in the table called paths, for example (/298/299/300) I get only the word referred to 300 but I need to get back all the three numbers.

My Model is:

class MuseumObject < ApplicationRecord
  belongs_to :path, :foreign_key  => "main_path_id"
end

class Path < ApplicationRecord
  has_one :termlist, through:  :termlist_paths
  has_many :museum_objects
  has_one :termlist_path
end

class TermlistPath < ApplicationRecord
  belongs_to :path
  belongs_to :termlist
end

class Termlist < ApplicationRecord
    belongs_to :path
    belongs_to :termlist_path
end

My Schema is:

  create_table "museum_objects", id: :serial, force: :cascade do |t|
    t.integer "main_path_id"
  end

  create_table "paths", force: :cascade do |t|
    t.string "path"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["path"], name: "index_paths_on_path", unique: true
  end 

  create_table "termlist_paths", force: :cascade do |t|
    t.bigint "termlist_id"
    t.bigint "path_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["path_id"], name: "index_termlist_paths_on_path_id"
    t.index ["termlist_id", "path_id"], name: "index_termlist_paths_on_termlist_id_and_path_id", unique: true
    t.index ["termlist_id"], name: "index_termlist_paths_on_termlist_id"
  end

  create_table "termlists", force: :cascade do |t|
    t.string "name"
    t.string "name_en"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "position"
    t.string "name_ar"
  end

  add_foreign_key "museum_object_paths", "paths"
  add_foreign_key "termlist_paths", "paths"
  add_foreign_key "termlist_paths", "termlists"
end

My Index is:

 <tbody>
    <% @museum_objects.each do |museum_object| %>
      <tr>       
         <td><%= museum_object.path.termlist_path.termlist.name_en%></td>          
        </tr>      
    <% end %>
  </tbody>
</table>

Photos of Tables in Postgresql

0 Answers
Related