Grouped Collection Select Alphabetical Order Rails
Option 1: In your City
model, add a default scope that directs cities to be returned in alphabetical order:
# app/models/city.rb
default_scope :order => 'cities.name ASC'
Collections of City
objects will, by default, be returned in alphabetically by name.
Option 2: Define a named scope in your State
model that returns cities in alphabetical order as an association on a State
object:
# app/models/state.rb
scope :cities_by_name, -> { cities.order(name: :asc) } # Rails 4
scope :cities_by_name, cities.order("name ASC") # Rails 3
Then, pass your scoped query to your grouped_collection
helper:
f.grouped_collection_select :city_id, State.order(:name), :cities_by_name, :name, :id, :name, {:include_blank=> "Select a City"}, {:class=>'dropdown'}
Rails - order by, with grouped_collection_select
Shot in the dark without seeing your models:
<%= f.grouped_collection_select :issue_id, School.all, :issues, :name, :id, :name, :include_blank => true %>
Then add a default scope to your issues model.
default_scope :order => 'issues.name ASC'
Order Alphabetically and Display By Group
titles.group_by {|word| word[0].upcase }
So, then if:
titles = ['Apple', 'anothersomething', 'Bob']
Then:
grouped_titles = titles.group_by {|word| word[0].upcase }
=> {"A"=>["Apple", "anothersomething"], "B"=>["Bob"]}
In order to be certain about the ordering, you can sort the resulting Hash, which converts it into an array:
grouped_titles = grouped_titles.sort_by{|k, v| k}
=> [["A", ["Apple", "anothersomething"]], ["B", ["Bob"]]]
Then you can iterate over the resulting array.
<% grouped_titles.each do |initial_letter, titles| %>
-display stuff here-
<% end %>
Note that this is grouping in Ruby, rather than in the database (which would be done by using a .group
method on the relation), but if you were already displaying all the data in the page, this method should be fine.
Order alphabetically and group by first letter
You should be able to do everything with your @alpha
instance variable, since you're using group_by
:
- @alpha = Glossary.find(:all, :order =>"title ASC").group_by{|u| u.title[0]}
- @alpha.each do |alpha, glossary_array|
%h1= alpha
- glossary_array.each do |item|
%p= item
options_for_select to sort in alphabetical order in ruby on rails
Actually the .sort method was misplaced so it was throwing me the error.
When I placed .sort method properly it worked as expected, which is shown as below;
options_for_select([['Z', 'Z'], ['T', 'T'], ['P', 'P'], ['B', 'B'], ['X', 'X'],['OTHERS', 'OTHERS']].sort)
rails group by and order by column
Try this, group_by
will return you hash
and you are trying to call [:sort]
on an Array of [#<Category id:2, sort:1>, [#<Extra id:3>,#<Extra id:4]]
You should take them in two variables |key, value|
and the key will be category
def index
@categories = Extra.all.group_by(&:category).sort_by{|category, _extras| category.sort }
end
Rails select_tag alphabetical order
after a hour all you have to do is .sort
the array of options that are passed into the options_for_select. this is my hack its a fix but not very sexy
select_tag :id, options_for_select(Portal.all.collect{|p| [p.name, portal_datum_path(p.id)]}.sort, [@portal.name, portal_datum_path(@portal)]), :onChange => "window.location=($(this).val());"
hope that helps
Custom ordering for query results from ActiveRecord association
You can specify the default order in the has_many
relationship.
class State < ActiveRecord::Base
has_many :areas, -> { order("areas.name ASC") }
end
Rails grouped collection select limit list
So I'm not sure where you are on this after previous comments and answer, but assuming your post above still stands, it looks like you're doing grouped_collection_select wrong. According to the docs, it should be more like
grouped_collection_select :event, :task_id, @workorderlist, @tasklist, :description, :id, :taskname
But part of the problem may be using an instance variable (@tasklist) when there should be a method there. The fourth element needs to be a group_method (= "The name of a method which, when called on a member of collection, returns an array of child objects representing the tags.") You can't call @tasklist on a member of your collection. So I would suggest making this an object method in your work_order model (assuming work_order has_many :tasks), like so:
class WorkOrder
def tasklist
tasks.where(<conditions>)
end
end
so that then you can actually call the method :tasklist in that fourth position there.
Related Topics
Script to Run Against Stdin If No Arg; Otherwise Input File =Argv[0]
How to Group Numbers into Different Buckets in Ruby
How Can One Set Property Values When Initializing an Object in Ruby
How to Add "Somewhere" a 'Before(:Each)' Hook So That All Spec File Can Run It
What's the "Ruby Way" to Parse a String for a Single Key/Value
How to Update Rails Locale Yaml File Without Loosing Comments and Variables
Need to Use Add_Index on Migration for Belongs_To/Has_Many Relationship? (Rails 3.2, Active Record)
Alphabetize Arabic and Japanese Text That Is in Unicode
How to Delete Some Folders of Nokogiri and Capybara-Webkit Inside of My Rvm Gemset
Why Is Ruby String.Hash Inconsistent Across MAChines
Shorter Way to Pass Every Element of an Array to a Function
How to Dynamically Define a Method as Private
What's the Purpose of Tainting Ruby Objects
How to Handle Utf-8 Email Headers (Like Subject:) Using Ruby
Why Does Ruby Only Permit Certain Operator Overloading