Question

I'm having some problems with a new feature I've added to my portfolio website. I have a number of portfolio entries, which I have now given categories. Everything is fine with the migration and that part works perfectly.

The only problem I am having is ordering the portfolio items in descending order in their respective categories.

Show.html.haml (for individual category)

-if @tag.portfolios.size > 0
  -@tag.portfolios.each do |portfolio|
   =link_to markdown(portfolio.content).first(72).html_safe, portfolio
  %br

 %p.text-right
  -if User.find_by_id(session[:user_id])
=link_to 'Edit', edit_portfolio_path(portfolio)
|
=link_to 'Delete', portfolio, method: :delete, data: { confirm: 'Are you sure?' }
  -else 
%br
-else
 %p Oops, I don't know what you're searching for - but it's not here!

Category controller

def index
 @tags = Tag.all
end

def show
 @portfolios = Portfolio.all( :order => "created_at DESC" )
end

I know it's something obvious, I just can't work out what.

Was it helpful?

Solution

It seems that you load @portfolios ordered, but you iterate over @tag.portfolios.
If you want Tag to associate portfolios in an ordered manner, see here:

class Tag
  has_many :portfolios, :order => "created_at DESC" 
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top