Home>
I want to mold multiple instance variables with jbuilder as follows.
Nested with two types of objects.
Nested with two types of objects.
{tasks: [{"id": 1, "name": "hoge", "is_done": true}, {"id": 2, "name": "huga", "is_done" : false}, {"id": 3, "name": "hogehuga", "is_done": true}], done_tasks: [{"id": 1, "name": "hoge", "is_done": true }, {"id": 3, "name": "hogehuga", "is_done": true}]}
Model
Task model
id: int
name: string
is_done: boolearn
def index
@tasks = Task.order ('updated_at DESC')
@done_tasks = Task.where (is_done: true)
end
I tried this pattern (@done_tasks class is Array)
@tasks = Task.order ('updated_at DESC')
@done_tasks = @ tasks.select {| task | task.is_done}
Tried with jbuilder
json.array! @tasks,: id,: name,: is_done,: created_at,: updated_at
end
json.done_tasks do
json.array! @done_tasks,: id,: name,: is_done,: created_at,: updated_at
end
json.set! tasks do
json.array! @tasks,: id,: name,: is_done,: created_at,: updated_at
end
json.set! done_tasks do
json.array! @done_tasks,: id,: name,: is_done,: created_at,: updated_at
end
json.set!: tasks do
json.array! @tasks do | task |
json.extract! task,: id,: name,: is_done,: created_at,: updated_at
end
end
json.set!: done_tasks done_tasks
json.array! @done_tasks do | done_task |
json.extract! done_task,: id,: name,: is_done,: created_at,: updated_at
end
end
etc ...
Current statusWhen you hit the corresponding curl command, it will be as follows or HTML will be returned.
{"tasks": [{"id": 1, "name": "hoge", "is_done": true}, {"id": 2, "name": "huga", " is_done ": false}, {" id ": 3," name ":" hogehuga "," is_done ": true}]," done_tasks ": []}
↑ done_tasks becomes an empty array
・ Method when@tasks
and@done_tasks
are both ActiveRecord
・@done_tasks
only for Array
-
Answer # 1
Related articles
- ruby on rails - unable to create controller
- ruby on rails - i can't create a model with rails
- ruby on rails - when calling the create action, the name attribute changes depending on whether it is indexhtmlerb or newhtmlerb
- ruby - i can't create a custom account with rails stripe
- ruby on rails - create a chat feature with action cable on ec2 on rails aws
- ruby on rails - i want to create an image posting function with react (typescript), formik, rails api, activestorage
- ruby - i can't "rails db: create rails_env = production" on aws
- ruby on rails - about rails create action
- ruby - [rails] i want to create an image link using the url of the image output using active storage
- ruby on rails 5 - rails: create an administrator screen using activeadmin colors of other pages change
- ruby on rails - i'm trying to create a container in docker but it doesn't work i want to create a container
- ruby - i want to transition from create to show with rails
- ruby on rails - not saved by create action
- ruby - unable to create rails model (environment: cloud9)
- ruby on rails - i want to create a like function with rails
- ruby on rails - i get an error with rails db: create
- ruby on rails 5 - rails: i want to create a timetable app
- ruby - [rails] i want to create a link including the data attribute with the link_to helper
- ruby on rails - how to debug with rails pry in docker environment
- ruby - unable to save comments in db (rails)
Related questions
- How can I perform operations only on numeric or string values when the depth of the hierarchy and key names are different each
- ruby on rails - [rails] i want to pass json from controller to html (erb)
- array - i want to display the json file line by line in processing
- about excel → json generation method from python
- ruby on rails uninitialized constant error
- javascript - i want to display the contents of an associative array and a combination of arrays
- i want to process into an array that combines the nth of json's two arrays with jq
- ruby on rails - extract multiple specific columns from one activerecord and make them json
When I applied rails restart, the changes of jbuilder were reflected and it worked.
It seems that the same result was repeated many times because the change was not reflected.