Friday, November 21, 2014

Rails On Firebird - status and follow the guide

Rails On Firebird - you need to check that Firebird active record adapter and ruby driver are installed
Install Rails 4.x :
 gem install rails
Generate a new Firebird Rails application
rails new firebird_app 
delete the generated database.yml sqlite config and use the one from bellow
Be aware about indentation
cat config/database.yml

Edit the project Gemfile and add the activerecord-fb-adapter gem:
gem 'activerecord-fb-adapter'
Then run:
bundle update
 rails generate scaffold Client name:string address:string email:string remark:text
  rake db:migrate
I have re-read the ruby guide
so i did these commands while following the official guide
rake db:create
but database was already created in flamerobin
script/generate controller home index
vi app/views/home/index.html.erb
and write something there, start the server with
rails s
and now you can see something in browser
http://localhost:3000/


I have created the table and one sequence in firebird db
CREATE TABLE posts (
id BIGINT NOT NULL PRIMARY KEY,
name VARCHAR(255),
title VARCHAR(255),
content VARCHAR(255),
timestamps timestamp
);
CREATE sequence POSTS_SEQ;

I have ran the scaffold again to generate the posts model/view/controller
script/generate scaffold Post name:string title:string content:text
and then
started the server
rails s

Of course I have modified the route and added an link as in tutorial and now i can add and modify blog posts
note: in new rails route is defined this way in config/routes.rb
map.root :controller => "home"

http://localhost:3000/posts






Follow the normal ActiveRecord conventions for table names, primary key columns, etc. The one “extra” you’ll need for Firebird is that you’ll have to create a generator for any tables that require a sequence-based primary key. The default naming convention is TABLENAME_SEQ. So if you have a users table, you would need to create a corresponding USERS_SEQ generator

You don't need to create before insert triggers ! rails reads the value from sequence and then increments it in ruby code, Yay!

6 comments:

Nielson Rolim said...

Hi Mariuz!

Great post! It's exactly what I needed.

I followed your tips, but I still got some issues.

I am using Firebird 2.5 and Rails 2.3.8. I've installed the to gems fb and fb_adapter and connected to the database successfully. But, when I tried to get an object from the database I've got the follow error:

>> p = Tbprod.first
ActiveRecord::StatementInvalid: Fb::Error: Invalid token
Dynamic SQL Error
SQL error code = -104
Token unknown - line 1, column 15
"TBPROD"
: SELECT * FROM "TBPROD" ROWS 1,
from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.8/lib/active_record/connection_adapters/abstract_adapter.rb:221:in `log'
from /Library/Ruby/Gems/1.8/gems/fb_adapter-0.6.0/lib/active_record/connection_adapters/fb_adapter.rb:307:in `log'
from /Library/Ruby/Gems/1.8/gems/fb_adapter-0.6.0/lib/active_record/connection_adapters/fb_adapter.rb:312:in `select_all'
from /Library/Ruby/Gems/1.8/gems/fb_adapter-0.6.0/lib/active_record/connection_adapters/fb_adapter.rb:299:in `translate'
from /Library/Ruby/Gems/1.8/gems/fb_adapter-0.6.0/lib/active_record/connection_adapters/fb_adapter.rb:311:in `select_all'
from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:664:in `find_by_sql'
from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:1578:in `find_every'
from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:1535:in `find_initial'
from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:616:in `find'
from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:626:in `first'
from (irb):2


Could you help me with that?

Popa Adrian Marius said...

Please install fb_adapter from git

Seems that you have a version of adapter that you doesn't give the first row (ROWS feature was implemented later )

I have tested with version from git and first works
p = Post.new(:content => "A new post")
=> #
>> p.save
=> true
>> f = Post.first
=> #

Popa Adrian Marius said...

The output was cleaned but here is on gist

https://gist.github.com/9dbec72ab99a70dcb9fa

I also requested a new version of the gem version

Popa Adrian Marius said...

The message is about TBPROD

check if it works with select * from TBPROD

My guess is that is something wrong with the model/table

Popa Adrian Marius said...

I never use dialect 1 but
rowland found the issue
I suspect a dialect 1 database and mixed-case table names are behind the errors. A backup/restore may solve Nielson's issue.
Actually, it turns out it is gfix which is required: http://www.firebirdsql.org/manual/gfix-dialect.html

Nielson Rolim said...

Thank you!

It is working fine now =)

I changed the dialect to 3.