Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs

Dashboard
Notifications
Mark all as read
Q&A
Parent

How to fetch single column for children? Question

+0
−0

How to get specific children's column? For questions, I can use @post.last_edited_at or, something just like this. But, I was thinking to fetch a single column for all child.

<% post.children.last_edited_at do |last_edited| %>
  <span>last_edited</span>
<% end %>

But, this isn't correct way to fetch single column for all child. I can fetch child's all column but, not a single one. I was thinking to put those strings inside an array. But, there's no possible way to put those value in array either. If I split using " than, what if user is using quotation mark in body.

<% children.each do |answer| %>

I can fetch all columns above way but....

I am working on post.rb models.

Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

Post
+1
−0

If I understand you correctly, you want to get the last_edited_at column for every child post, i.e. a list of last edit dates?

For this you should use

post.children.map(&:last_edited_at)

which is a shorthand for

post.children.map { |c| c.last_edited_at }

which iterates over the list of children and maps each to its last_edited_at timestamp.

Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

not working properly (2 comments)

Comments on How to fetch single column for children?

not working properly
Istiak‭ wrote 3 months ago:

When I used post.children.map(&:last_edited_at) I got the following error.

Error : ActionView::Template::Error: undefined local variable or method `post' for #Post:0x00007fb57e09e8f0

Using @post I got the following error.

Error : ActionView::Template::Error: undefined method `children' for nil:NilClass

I am working on post.rb in models

Istiak‭ wrote 3 months ago:
children.map(&:last_edited_at)

above code is working properly in post.rb

This community is part of the Codidact network. We have other communities too — take a look!

You can also join us in chat!

Want to advertise this community? Use our templates!

Like what we're doing? Support us! Donate