how to sort joined tables?

20 views
Skip to first unread message

Zlodiak Zlodiak

unread,
Jun 25, 2015, 6:57:30 AM6/25/15
to rubyonra...@googlegroups.com
help please write orm-request.

tables:
users:
id: integer
name: varchar

posts:
id: integer
title: varchar
user_id: integer
views: integer

models:
User:
class User < ActiveRecord::Base
has_many :posts, dependent: :destroy
end

Posts:
class Post < ActiveRecord::Base
belongs_to :user
end

controller:
def popular_diary
@users =
User.joins(:posts).group_by(:user_id).order('SUM(posts.views)')
end

html:
<% @users.each do |user| %>
<div class="row">
<%= link_to user_posts_url(user.id) do %>
<div><%= user.name %></div>
<% end %>
</div>
<% end %>

I need that all users hatched in a certain order. order ('SUM
(posts.views)

The problem is that the screen displays the following error message:
wrong number of arguments (0 for 1)

--
Posted via http://www.ruby-forum.com/.

tamouse pontiki

unread,
Jun 25, 2015, 7:01:45 AM6/25/15
to rubyonra...@googlegroups.com
Look near the error message it will show you what line the error occurred at.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/12a9d6c292fa4a6ff54993fcee9a1c36%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.

Zlodiak Zlodiak

unread,
Jun 25, 2015, 7:06:49 AM6/25/15
to rubyonra...@googlegroups.com
tamouse m. wrote in post #1175655:
> Look near the error message it will show you what line the error
> occurred
> at.
>
> On Thu, Jun 25, 2015 at 5:56 AM, Zlodiak Zlodiak <li...@ruby-forum.com>

http://joxi.ru/1A5RvGah55YwrE

Zlodiak Zlodiak

unread,
Jun 25, 2015, 7:30:42 AM6/25/15
to rubyonra...@googlegroups.com
sorry. i replace 'group_by' on 'group' and it worked)
Reply all
Reply to author
Forward
0 new messages