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

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -