Rails association collection using downcase - ruby-on-rails

I have the following Rails view/form association:
<%= f.association :workorder, :collection => Workorder.wonotclosed.order(:description), :label_method => :description, :label => 'Project' %>
The issue is the workorders are sorted using uppercase first and then lowercase. I would like it to be case insensitive.
I tried these:
:collection => Workorder.wonotclosed.order(LOWER(description))
:collection => Workorder.wonotclosed.order(:description.downcase)
Thanks for the help!

You need to pass the SQL snippet in as a string:
Workorder.wonotclosed.order("LOWER(description)")

Related

Formtastic - Why does not work

I have tried to use this on my test
<%= f.input :user, :label => 'Usuario: ' , :as => :select , :collection => #users , :include_blank => true %>
And didnt work....
But when a switch to:
<%= f.input :user_id, :label => 'Usuario: ' , :as => :select, :collection => Hash[#users.map{|b| [b.nickname,b.id]}] , :include_blank => true%>
It does work? Does anyone knows why?
I also put it all together on the code:
<%= f.input :user, :label => 'Usuario: ' , :as => :select , :collection => #users , :include_blank => true %>
<%= f.input :user_id, :label => 'Usuario: ' , :as => :select, :collection => Hash[#users.map{|b| [b.nickname,b.id]}] , :include_blank => true%>
But the first one didnt work (i did just to see if was somente wrong in the #user variable)..
Is something related to the model? Does anyone knows why?
From formtastic documentation: Many inputs provide a collection of options to choose from (like :select, :radio, :check_boxes, :boolean). In many cases, Formtastic can find choices through the model associations, but if you want to use your own set of choices, the :collection option is what you want. You can pass in an Array of objects, an array of Strings, a Hash…
You can check the documentation
also collection is expect to receive a hash or array , but when you pass #user you pass an instance variable of your model and that will not work.

Rails/Formtastic Newbie: Setting display field for referenced "select" box

New formtastic user here.
I have a relationship user has_many clients
In formtastic, if I do something such as
f.input :employer
it returns a select box of all employer object references. I'd like to display (last name, first name) instead. I'm sure this is very simple, but I can't figure out exactly how to do it.. Any help appreciated.
These didnt work for me, however, this did:
<%= f.input :user, :label => "Author", :label_method => :username %>
Also a little cleaner ^^
Or, you can set the display method once and for all on the model itself:
(In employer.rb)
def to_label
email
end
Try
f.input :employers, :as => :select, :collection => Employer.find(:all, :order => "last_name ASC")
or
f.input :employers, :as => :select, :collection => Employer.collect {|e| [e.last_name, e.id] }

How to properly pass collection for input in Formtastic

I need to pass a collection to the standard select input in Formtastic:
f.input :apple, :as => :select, :collection => Apple.all
The problem is, though that I need Formtastic to access a different method than name. Now this is really a problem. I can always pass the Array
f.input :apple, :as => :select, :collection => Apple.map { |a| a.format_name }
The problem is, that after this I will get strings in the controller instead of IDs which is not desired. I tried to pass Hash instead:
options = Hash.new
Apple.each { |a| Apple.store(a.format_name, a.id) }
f.input :apple, :as => :select, :collection => options
Now the problem is, that since I am using Ruby 1.8.7, the Hash order is unspecified and I of course need ordered input...
I can imagine some solutions, but all of those require unnecessary code.
Any idea how to solve this right?
Try:
f.input :apple, :as => :select, :collection => Apple.all, :label_method => :format_name, :value_method => :id
There is no direct indication in the formtastic documentation, but collection can be nested arrays as well, so problem is solved by:
f.input :apple, :as => :select, :collection => Apple.map { |a| [ a.format_name, a.id ] }
This is the correct way now:
f.input :apple,
as: :select,
collection: Apple.pluck(:format_name, :id)
This sets collection to an array of [name, id] tuples. Easy!
Soon-to-be deprecated way:
Use the member_label option, e.g.
f.input :apple,
as: :select,
collection: Apple.all,
member_label: :format_name
Documentation is here in a code comment.

Rails: How to use select in formtastic with activeRecord

I'm new to rails and I guess you can answer this question easily.
What I got so far is
= f.input :task, :as => :select, :collection => #tasks, :include_blank => true
where the tasks collection is defined by
Task.find(:all)
within in the controller.
This does in fact work, shows me a dropdown-list of all Tasks to select from and connects them.
The problem here is, that the dropdown menu shows me values like
#<Task:0x123456789d1234>
Where do I define what value is being displayed?
I believe you can use the :label_method to solve your problem...
f.input :task, :as => :select, :collection => #tasks,
:include_blank => true, :label_method => :title
where :title is what you want to show.
This may help a little more.
You can define a to_s method in the model:
class Task < ActiveRecord::Base
def to_s
title # the attribute to display for the label
end
end

Using :collection and :include_blank in Formtastic. How to do it?

I use the superb Formtastic plugin for Ruby on Rails.
Does anybody know how to include a blank (option), when using a custom collection?
When I try:
<%= f.input :organizations, :collection => Organization.all(:order => :name), :include_blank => true %>
I get the select box with the collection, but NOT a blank...
What kind of association is :organizations? Does it work if you specify :as => :select?
There's spec coverage for the following belongs_to select, date, time and datetime inputs:
f.input(:author, :as => :select, :include_blank => true)
f.input(:created_at, :as => :date, :include_blank => true)
f.input(:created_at, :as => :time, :include_blank => true)
f.input(:created_at, :as => :datetime, :include_blank => true)
My guess is that organizations is not a belongs_to association, right? If it's a :has_many or :has_and_belongs_to_many association, Formtastic will try to do checkboxes or a multi-select. In the case of a multi-select, obviously it makes no sense to have a blank line in there (you just don't select any of the items).
Hope this helps, please post some more details about the models and associations in question.

Resources