I'm trying to find a method to take a particular relation and move it to the end of the array. Basically, I have a current_account
and I want to take this account and move it to the end of the account relationship array so that it will display last when I iteration over the relationships. I want to make a scope and use SQL if possible, here is my attempt and I haven't really gotten anywhere.
HTML
<% current_user.accounts.current_sort(current_account).each do |account| %>
<li><%= link_to account.name, switch_account_accounts_path(account_id: account.id) %></li>
<% end %>
This current return a list of sorted by created_at accounts. I don't want it to be sorted by created at but the current_account
to be at the bottom so I make a scope called current_sort
but I'm not sure what to do here.
CURRENT_SORT SCOPE ON ACCOUNT
scope :current_sort, lambda { |account|
}
I want this scope to return the passed in account last in the association array. How can I do this with SQL or Ruby?
sort_by { |v| v == current_account ? 1 : 0 }
? – Moshelist - [ current_account ] + [ current_account ]
but that seems more messy. – Moshe