Hi Megan

I think handling it in the model, as Dan and Chris suggest, is correct.
 That is fine as long as you make sure that happens EVERY time a title is
saved into the DB.  As you noted, sorting will not work (as you want) if
some titles are capitalized and other are not.  Doing it at the model level
almost ensures that will always happen.

Another option is to sort the titles via SQL and not worry about storing
them capitalized or not.  This is assuming you have a SQL query to fetch
the ordered collection.

def self.sort_by_title
  order("LOWER(title)")
end

If not you can always sort using ruby:

[<collection of objects>].sort{|x| x.title.downcase <=> x.title.downcase}

You can call capitalize on the title attribute to make sure it is always
capitalized when displayed.  This way you do not care how it is stored in
your DB

def title
  self['title'].capitalize if self['title'].present?
end

Personally, I like the 2nd option.  If in the future, titles get stored in
your DB in a different place, you do not have to worry about always
remembering to capitalize them.  Also it guards against someone manually
entering a non-capitalized title in your DB and messing up your sorting.

Cheers

Ben W


On Wed, Apr 30, 2014 at 9:42 AM, Megan Byrne <[email protected]>wrote:

> Hello all,
>
> I'm relatively new to RoR and have a quick question.
>
> I want to make sure that the :title of wikis that are submitted through a
> form are always converted to capital letters so that they can be sorted
> properly.  Currently they are sorted alphabetically, but this becomes a
> problem if people do not capitalize their first letter (ie. uppercase
> letters sorted first, then lowercase sorted next).
>
> I know that I want to do something like wiki.title.titleize, which is a
> great RoR extension, but where do I put this?  Is it something I write into
> the create and update methods in the controller?  Or, is it something I
> write in the model?
>
>
> --
> --
> SD Ruby mailing list
> [email protected]
> http://groups.google.com/group/sdruby
> ---
> You received this message because you are subscribed to the Google Groups
> "SD Ruby" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>

-- 
-- 
SD Ruby mailing list
[email protected]
http://groups.google.com/group/sdruby
--- 
You received this message because you are subscribed to the Google Groups "SD 
Ruby" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to