Ruby on Rails
Thursday, December 30, 2010
> I just created a migration which changes my default values for string and text fields to empty string rather than nil.Do it in the migration itself... as sql... it's simpler, it's self contained, it's right there along side the code that changes the default...
>
> I need to update all existing records where a string or text field in this table is nil. This is what I am trying but just curious if there is a better way, I don't quite have it working and just seems like there might be a better way:
%w[text_field1 text_field2].each do |field|
TravelCard.update_all("#{field} = ''", "#{field} IS NULL")
end
The only reason not to do the above is if you can't make the change at the same time you alter the defaults.
But like Marnen said, I'd take a good look at why you think this change is necessary... There are some benefits to being able to distinguish between empty strings and null values...
Ok, actually I would like your ideas. The issue started with a problem in a view where I am using the field value to determine what css class I use on an element. Things were fine until I got a nil value out of the db because of course field#downcase fails if nil. So the conclusion I came to is that I dont want to have to add disgusting stuff to the view like:
<span class="gateway_selector_side_highlight_<%= @travel_card.dominant_occiput.downcase if @travel_card.dominant_occiput %>"></span>
and rather have
<span class="gateway_selector_side_highlight_<%= @travel_card.dominant_occiput.downcase %>"></span>
So that is where I realized I could either write logic in the model (more work) or set a default db value which would seem less work. I am not in love with the idea of default db values, never really have been, but it seemed like the best place. Basically in this case nil or empty string mean the same to me.
Is there a better option?
Thinking aloud I guess I could add a helper method to handle the value generation... which now in retrospect does seem easier than this kind of disgusting db migration
Yep! Helper method for the win :)
-philip
-philip
> # model I am updating is TravelCard
> travel_card_columns_hash = TravelCard.columns_hash
> TravelCard.all.each do |travel_card|
> travel_card_columns_hash.keys.each do |key|
> if travel_card_columns_hash[key].type.downcase == 'string' || travel_card_columns_hash[key].type.downcase == 'text'
> if !eval('travel_card.key')
> travel_card.key = ''
> end
> end
> end
> end
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment