I Display it mainly. On a variety of controllers.
If it's just for display, don't strip out spaces. Store the actual name, then define a method username
which removes spaces from name.
Something like following should work:
# Model
class Show < ActiveRecord::Base
# Returns name removing all spaces
def username
self.name.gsub(/\s+/, "")
end
# Remove just the leading and trailing white spaces
def name=(value)
write_attribute(:name, value.strip)
end
end
Then in your controller(s):
<%= link_to @show.username, @show %>
Update:
After having a chat with OP, understood that he is using to_param
method in Show
model which returned username
(i.e. name
with all spaces removed). This caused a problem in find
method because the value in DB included spaces and the value being searched for did not have any spaces, as a result of which a DB specific function needed to be used e.g. where("replace(name, ' ', '') = ?", user_name)
in case of MySQL. Since the OP is using different databases in development and production, i.e. SQLite in development and PG in production. So, instead of crafting a solution that worked across different databases, recommended OP to use FriendlyId gem.