B
B
bismoney2015-11-23 10:29:59
Ruby on Rails
bismoney, 2015-11-23 10:29:59

How to display posts for a project?

Hi friends!
I can not understand in any way, I have a problem with these requests.
I honestly tried to solve it myself, but I didn’t get it.
Selection of projects

@posts_project = 
      Project
        .limit(3)
        .order(created_at: :desc)

Which outputs 3 projects.
How now from the post table to display 5 news for each of the 3 projects?
And to make it so that if, when adding fresh news to the project, this project would come first?
Use join?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
N. Bekseitov, 2015-11-23
@bismoney

@projects = Project.limit(3).order(created_at: :desc)
@projects.each do |project| 
  project.posts.limit(5).each {|post| @posts << post}
end
@posts_project = @posts.order(created_at: :desc)

and don't forget to include has many :posts relationships for the project model

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question