0

我有这样的代码:

user.posts.size #=> 5 already saved
user.posts.new(title:"foo")
user.posts.new(title:"bar")
user.posts.sort_by! { |e| e.title } #=> sort correclty
user.posts #=> sorted but with the saved on the top, the new one at the bottom

我使用排序,field_for但显然它仍然未排序。

使用:Rails 3.2.11 和 Mongoid 3.0.23

4

1 回答 1

1

我不确定为什么会这样,但我认为这会奏效

# controller
user.posts.build(title:"foo")
user.posts.build(title:"bar")

@posts = user.posts.sort_by(&:title)

# view
= f.fields_for :posts, @posts do |post_form|
  ...
于 2013-04-09T07:39:27.167 回答