I have a rails model let say
User with a has many relationship with Post. Post contains an attribute named position that stores the position (acts_as_list gem). The position stores the position on the user Post list (the order is not due to creation/modification of the post).
Moreover, the Post has timestamps stored (t.timestamps
in the migration)
So I have defined the following in User.rb:
has_many :post, :order => "position", :dependent => :destroy
However, when i do a User.first.posts
The posts are not ordered by position.
I've cheked and the sql being queries contains the following:
ORDER BY post.created_at DESC, position
So the created_at is still being used.
How can I sort by position and not use the created_at attribute?
Thanks in advance!
SOLUTION I had a default scope degined in Post that was creating the undesired order by.
Problem solved
You can use a default scope:
class Post < ActiveRecord::Base
scope :by_position, order("position ASC")
default_scope by_position
#...
end
Then @user.posts
should return a list ordered by the position (ASC).
Also, the following should be working for you: (plural on post
)
has_many :posts, :dependent => :destroy, :order => "posts.position ASC"