I am beginner in Ruby-on-Rails and I started to investigate ActiveRecords. And I run into one problem.
I have two tables: todo_lists and users. The first one has following records: title, description, user_id, deadline. And the second one has only one record: name. I want to get a table that contains following records: title, description, name, deadline, i.e. combine two tables and put user instead of user_id. I try to solve this problem using joins:
TodoList.joins(:user).select("todo_lists.title, todo_lists.description, users.name, todo_lists.deadline")
But I get the new ActiveRecord::Relation without users.name:
[#<TodoList id: nil, title: "This is title", description: "This is description", deadline: "2020-03-13 18:59:58">]
This is my todo_list.rb and user.rb files:
class TodoList < ApplicationRecord
belongs_to :user
end
class User < ApplicationRecord
has_many :todo_lists
end
schema.rb file:
ActiveRecord::Schema.define(version: 2020_03_13_183504) do
create_table "todo_lists", force: :cascade do |t|
t.string "description", null: false
t.integer "user_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.boolean "is_disabled"
t.datetime "deadline"
end
create_table "users", force: :cascade do |t|
t.string "name", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
add_foreign_key "todo_lists", "users"
end
Can anyone help me to solve this problem?