Как я получаю данные из таблицы соединений. У меня есть три модели: Пользователь, Ребенок и Задача. У пользователя много дочерних и заданий. А у Дитя много задач и принадлежит пользователю. И Задача принадлежит пользователю и ребенку. Я хочу получить пользователя со всеми дочерними элементами и задачами с дочерним именем. Как я получаю дочернее имя в массиве объекта задачи. Мод
// Parent Model
class User < ApplicationRecord
has_many :task, dependent: :destroy
has_many :children
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
end
// Child Model
class Child < ApplicationRecord
belongs_to :user
has_many :task
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
end
// Task Model
class Task < ApplicationRecord
belongs_to :user
belongs_to :child
end
В методе show
def show
user = User.find(params[:id]);
data = {
user:user,
childrens:user.children,
tasks:user.task
}
render json:{status:'success', error:false, data:data},status: :ok
end
это работает, и вывод
{
"status": "success",
"error": false,
"data": {
"user": {
"id": 2,
"full_name": "sohail ahmad",
"email": "sohail1@gmail.com",
"created_at": "2020-03-10T05:41:42.045Z",
"updated_at": "2020-03-10T05:41:42.045Z"
},
"childrens": [
{
"id": 4,
"user_id": 2,
"child_name": "child 1",
"email": "child1@gmail.com",
"created_at": "2020-03-10T13:52:12.896Z",
"updated_at": "2020-03-10T13:52:12.896Z"
},
{
"id": 5,
"user_id": 2,
"child_name": "child 2",
"email": "child2@gmail.com",
"created_at": "2020-03-10T13:52:22.910Z",
"updated_at": "2020-03-10T13:52:22.910Z"
},
{
"id": 6,
"user_id": 2,
"child_name": "child 3",
"email": "child3@gmail.com",
"created_at": "2020-03-10T13:52:28.771Z",
"updated_at": "2020-03-10T13:52:28.771Z"
}
],
"tasks": [
{
"id": 1,
"task_name": "first task",
"date_assigned": "2020-03-10",
"points_to_earn": 10,
"instructions": "dummy instructions",
"child_id": 1,
"user_id": 2,
"status": 0,
"created_at": "2020-03-10T13:50:43.196Z",
"updated_at": "2020-03-10T13:50:43.196Z"
},
{
"id": 2,
"task_name": "first task",
"date_assigned": "2020-03-10",
"points_to_earn": 10,
"instructions": "dummy instructions",
"child_id": 4,
"user_id": 2,
"status": 0,
"created_at": "2020-03-10T13:52:49.850Z",
"updated_at": "2020-03-10T13:52:49.850Z"
},
{
"id": 3,
"task_name": "first task",
"date_assigned": "2020-03-10",
"points_to_earn": 10,
"instructions": "dummy instructions",
"child_id": 5,
"user_id": 2,
"status": 0,
"created_at": "2020-03-10T13:52:56.298Z",
"updated_at": "2020-03-10T13:52:56.298Z"
},
{
"id": 4,
"task_name": "first task",
"date_assigned": "2020-03-10",
"points_to_earn": 10,
"instructions": "dummy instructions",
"child_id": 6,
"user_id": 2,
"status": 0,
"created_at": "2020-03-10T13:53:06.922Z",
"updated_at": "2020-03-10T13:53:06.922Z"
},
{
"id": 5,
"task_name": "first task",
"date_assigned": "2020-03-10",
"points_to_earn": 10,
"instructions": "dummy instructions",
"child_id": 6,
"user_id": 2,
"status": 0,
"created_at": "2020-03-10T13:53:16.174Z",
"updated_at": "2020-03-10T13:53:16.174Z"
}
]
}
}
Как получить дочернее имя в массиве объекта задачи, как это
"tasks": [
{
"child_name": "Chlid 1",
"id": 1,
"task_name": "first task",
"date_assigned": "2020-03-10",
"points_to_earn": 10,
"instructions": "dummy instructions",
"child_id": 4,
"user_id": 2,
"status": 0,
"created_at": "2020-03-10T13:50:43.196Z",
"updated_at": "2020-03-10T13:50:43.196Z"
},
]