June 30th, 2008 by Leons Petrazickis
I ran into a tiny pickle while installing the ibm_db gem on Ubuntu:
Select which gem to install
for your platform
(i486-linux
)
1.
ibm_db 0.9.5 (ruby
)
2.
ibm_db 0.9.5 (mswin32
)
3.
ibm_db 0.9.4 (ruby
)
4.
ibm_db 0.9.4 (mswin32
)
5.
Skip this gem
6.
Cancel installation
>
1
Building native extensions.
This could take a
while…
ERROR:
While executing gem …
(Gem::Installer::ExtensionBuildError
)
ERROR: Failed to build gem native extension.
ruby extconf.rb install ibm_db
extconf.rb:9:in `require’: no such file to load — mkmf (LoadError)
from extconf.rb:9
The problem is that mkmf isn’t included in the base Ruby package on Ubuntu. You need need the full development package to install gems from source. Installing it solves the problem.
sudo apt-get install ruby1.8-dev
Posted in db2, ruby | 4 Comments »
May 28th, 2007 by Leons Petrazickis
When creating a table in a Rails migration, you have to specify data types using platform-agnostic names. The mapping of Rails types onto DB2 types is defined in ibm_db_adapter.rb:
:primary_key => @servertype.
primary_key,
:
string =>
{ :name =>
"varchar", :limit =>
255 },
:text =>
{ :name =>
"clob" },
:
integer =>
{ :name =>
"integer" },
:
float =>
{ :name =>
"float" },
:datetime =>
{ :name =>
"timestamp" },
:timestamp =>
{ :name =>
"timestamp" },
:time =>
{ :name =>
"time" },
:date =>
{ :name =>
"date" },
:binary =>
{ :name =>
"blob" },
# A boolean can be represented by a smallint,
# adopting the convention that False is 0 and True is 1
:boolean => { :name => "smallint"},
:xml => { :name => "xml"},
:decimal => { :name => "decimal" }
Useful Resources
InfoCenter | DB2 Data Types
dW | DB2 and Ruby on Rails, Part 1 (May 2007)
dW | An Introduction to Ruby on Rails for DB2 Developers (June 2006)
The DB2 adapter is now called ibm_db. You can refresh your installation by typing gem install ibm_db at the command line and choosing the latest win32 release.
Posted in db2, ruby | No Comments »
May 4th, 2007 by Leons Petrazickis
After a successful form entry, a Rails scaffold redirects you to the list view of all items. This is controlled by app/controllers/customers_controller.rb, where “customers” is the name of the scaffold. You can easily change it to redirect to the edit view or the show view.
def create
@customer = Customer.new(params[:customer])
if @customer.save
flash[:notice] = ‘Customer was successfully created.’
redirect_to :action => ‘list’
else
render :action => ‘new’
end
end
You want to change the redirect action from:
redirect_to :action => ‘list’
to
redirect_to :action => ’show’, :id => @customer
where ’show’ is the view that you want and customer is your model.
Posted in ruby | No Comments »
March 27th, 2007 by Leons Petrazickis
There is a simple way to make a drop-down list of constants in a Rails form. Such lists are needed for months, provinces, weekdays, and other enum-like concepts. You don’t need to mess around with multiple tables and the corresponding belongs_to, has_one, has_many, and has_and_belongs_to_many relations.
First of all, add your list to your model in app/models/:
class MyModel < ActiveRecord::Base
def self.MyList
["British Columbia",
"Alberta",
"Saskatchewan",
"Manitoba",
"Ontario",
"Quebec",
"Nova Scotia",
"New Brunswick",
"Prince Edward Island",
"Newfoundland and Labrador",
"Nunavut",
"Northwest Territories",
"Yukon"]
end
end
Then, change the Province textbox in app/views/MyView/_form.rhtml to:
<p><label for="MyModelTable_MyColumn">MyColumn</label>
<%= select("MyModelTable", "MyColumn", MyModel.MyList) %>
The list will now show up as a dropdown. When editing, the appropriate value will be selected.
References:
Posted in ruby | 2 Comments »