ruby on rails - Show the latest content in the view by created at date -
ruby on rails - Show the latest content in the view by created at date -
i able sort users created_at
date using @users.sort_by(&:created_at).reverse.each |user|
i need help sorting subscribe users subscription created_at
date.
<% @users.each |user| %> <li> <%= link_to user.username, user %> <% if user.subscription %> |<%= user.subscription.id %> | <%= user.subscription.plan.name %> | <%= user.subscription.created_at.strftime("%b %e, %y") %> | <% if user.subscription.stripe_customer_token.nil? %> paypal <% end %> <% if user.subscription.paypal_recurring_profile_token.nil? %> stripe <% end %>
i think need alter of code in view, should showing users within subscription table. instead pulling users table, , grabbing subscription information. current code of course of study showing users without subscriptions.
sort_by can take block.
@users.sort_by {|user| user.subscription.created_at}
http://ruby-doc.org/core-2.1.3/enumerable.html#method-i-sort_by
just set in chain
@users.sort_by {|user| user.subscription.created_at}.reverse.each |user|
this works assuming subscription belongs_to user , user either has_one, or has_many subscriptions. can tell view does.
ruby-on-rails
Comments
Post a Comment