Posted by Chris Blackburn
Thu, 19 Jun 2008 01:01:00 GMT
schema.rb for states table
create_table "states", :force => true do |t|
t.string "name"
t.string "abbreviation", :limit => 2
t.datetime "created_at"
t.datetime "updated_at"
end
cities_controller.rb
def new
@city = City.new
@states = State.find(:all, :order => :abbreviation)
respond_to do |format|
format.html
format.xml { render :xml => @city }
end
end
new.html.erb view for cities_controller.rb
<% form_for(@city) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :state_id %><br />
<%= f.collection_select(:state_id, @states, "id", "abbreviation") %>
</p>
<p>
<%= f.submit "Create" %>
</p>
<% end %>
Posted in Ruby | Tags collection, rails, rubyonrails, select, selector, snippets, state | no comments | no trackbacks
Posted by Chris Blackburn
Thu, 31 May 2007 18:10:00 GMT
If you are experiencing the error: “undefined class/module MyClass” when fetching data from memcached, be assured that you are not alone. It is a known bug and the simplest way I know of to get around it is to reference the class or classes right before you retrieve data from the cache.
For example, if the following code causes the problem:
if not (genres = Cache.get(key))
genres = Genre.find(:all, :condition => "platform_id = 1")
Cache.put(key, genres, 60*60*24)
end
… then this code will work around it:
Genre
if not (genres = Cache.get(key))
genres = Genre.find(:all, :condition => "platform_id = 1")
Cache.put(key, genres, 60*60*24)
end
Notice the ‘Genre’ reference before the if statement. Some have reported success by using the ‘model’ statement within the controller, however that is deprecated. This workaround will get you going again.
Tags bugs, errors, memcached, ruby, rubyonrails, snippets, workarounds | 6 comments | no trackbacks
Posted by Chris Blackburn
Wed, 23 May 2007 19:40:00 GMT
I am fairly anal about my code and the code that my firm (CBCI) produces. I like it to be formatted perfectly. It makes me cringe to even have a single extra space out of place, borking the indentation. Using tabs for indentation instead of spaces should be grounds for termination. :-D
In addition to perfect indentation, I don’t like scrolling to the right in my editor to view long lines of code. This can usually be handled properly by adding linefeeds. Consider the following example:
sql = %Q{
SELECT
users.id,
users.username,
users.email,
users.created_at,
users.activated_at,
users.confirmed_at,
affiliations.custom1 as affiliation_custom1,
affiliations.custom2 as affiliation_custom2,
affiliations.custom3 as affiliation_custom3,
campaigns.name as affiliation_campaign_name
FROM users
LEFT OUTER JOIN affiliations ON affiliations.user_id = users.id
LEFT OUTER JOIN campaigns ON campaigns.id = affiliations.campaign_id
}
There are several ways to handle this type of long string. First, for SQL statements, extra spaces in front of each line don’t matter, nor do the linefeeds. So the code above, using the ‘%Q’ method, would work just fine for building a string with spaces embedded. We could also use the lowercase ‘%q’ method which would wrap the string with single-quotes and prevent any escape sequences or interpolation of embedded expressions, which is fine since we don’t have any need for that in the above string. You could also wrap the quotation marks, single or double, around the string explicitly.
If you are working with a long character string, and are fussy about your code being formatted consistently, the indents will embed groups of spaces into your strings. As stated above, this technically doesn’t matter when considering SQL statements. In other cases where it does matter we use the Ruby HERE document as follows:
class String
def here_with_pipe
lines = self.split("\n")
lines.map! {|c| c.sub!(/\s*\|/, '')}
new_string = lines.join(" ")
self.replace(new_string)
end
end
This strips off the leading whitespace and pipe character that gets embedded in the string using the HERE document like this:
s = <<-end.here_with_pipe
|This string will be concatenated
|into a single line
|containing no extra embedded spaces
|and no line feeds.
end
Isn’t Ruby cool!
Tags formatting, ruby, snippets, strings | 1 comment | no trackbacks
Posted by Chris Blackburn
Tue, 22 May 2007 22:52:00 GMT
Have you ever wanted to look at the call stack without raising an exception to do it?
Tags debug, howto, rubyonrails, snippets | no comments | no trackbacks