开发者

Why is my controller not ordering by what Im telling to order by?

开发者 https://www.devze.com 2023-04-06 21:06 出处:网络
My controller action is calling all images belonging to a specific user and Im trying to order by its position (Im using the acts_as_list gem) but when I go to the page, the images are FIRST sorted by

My controller action is calling all images belonging to a specific user and Im trying to order by its position (Im using the acts_as_list gem) but when I go to the page, the images are FIRST sorted by the created date, and then position (according to rails console). But because it orders by the creation date first my controller order is being ignored which is no good.

here is my action

  def manage_tattoo开发者_StackOverflow社区s
    @tattoos = current_member.tattoos.order("position DESC")
  end

and my server console shows:

  Tattoo Load (0.6ms)  SELECT `tattoos`.* FROM `tattoos` WHERE
 (`tattoos`.member_id = 1) ORDER BY tattoos.created_at DESC, position DESC


Have you tried specifiing the order in the association?

 class TodoList < ActiveRecord::Base
   has_many :todo_items, :order => "position"
 end


Does your association between Member and Tattoo have an order clause? E.g.

# Member class
has_many :tattoos, :order => "created_at DESC"

Is this the case? If so you might need to change your query to something like:

Tattoo.where(:member_id=>current_member.id).order("position DESC")

I'm unaware of a way to clear the order clause from an ActiveRecord association.


Or specify what to do with created_at:

current_member.tattoos.order("position DESC, created_at DESC")


Well it seems Im just very absent minded and had completely forgotten that I set a default scope on the model. Took that out and everything is fine

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号