首次海报。 我想排序使用洗劫的宝石和雷的分页用户的表。 当我使用的名称,ID等排序的作品,但是当我尝试的关联与POSTS_COUNT,分拣休息和将无法正常工作。 注:在视图中,“u.posts.count”正常工作。 我试图在用户模式自定义范围,并创建自定义对象的搜索PARAMS,但似乎没有任何工作。 我觉得我有无论是在默认范围或不具有数据@search对象的麻烦。 需要帮忙!
下面是一些相关的片段:
车型/ user.rb
has_many :posts, :dependent => :destroy
车型/ post.rb
belongs_to :user
default_scope :order => 'post.created_at DESC'
控制器/ users_controller.rb
def index
@title = "User Index"
@search = User.search(params[:q]) # Ransack
@total_users = User.all.count
# .per(10) is the per page for pagination (Kaminari).
@users = @search.result.order("updated_at DESC").page(params[:page]).per(10) #This displays the users that are in the search criteria, paginated.
end
意见/用户/ index.html.erb
..
<%= sort_link @search, :posts_count, "No. of Posts" %> #Sort links at column headers
..
<% @users.each do |u| %> #Display everything in the table
<%= u.posts.count %>
<% end %>