Ruby Screenshot of the Week #21: ActiveRecord Completion

Let's jump to the good stuff right away:

Okay, now let's motivate it. Let's say you're writing a migration:

Hmm, what's the second parameter to the column method again?

Ah yes, the column type. Notice how the RDoc for the method call surrounding
the completion point is shown above - and perhaps more importantly, the symbol alternatives for
the current parameter are also
proposed below. Let's choose one.

Ah yes, the third parameter - the options. Again the documentation is
shown above (I've cropped it in this image) where you can read the details -
but many of the alternatives are listed here. Let's choose the :null hash key.

The parameter completion support I've shown here isn't specific to ActiveRecord. Let's say you're in
an ERB file and calling into say the NumberHelpers:

Anyway, we're done editing the migration. Now let's jump over to a controller file and reference the Product model that is using the database table for this migration. Let's ask for completion on the @product field that was just populated with a Product object:

The icon should make it really clear that these attributes are coming from the database as opposed to some dedicated attribute code in the Product implementation. Notice how NetBeans also shows the type for each of the columns. Completion also works for the dynamic finders that Rails generates. Let's ask for completion on find_by (this also works for find_all_by):

NetBeans offers code completion for models by examining the migration files. Let's go create another one. Here's
completion again, this time completing on the table name argument to rename_column:

Let's say we rename the description column to desc:

If we now invoke code completion in the controller again, notice how the Product attributes correctly
reflect the result of combining the migrations:

NetBeans will also use the schema.rb file that Rails will automatically generate if you run the db:schema:dump Rake target. This is useful if your migrations are doing creative things that NetBeans can't figure out, or if you're renaming tables (which NetBeans doesn't model right in this release.) With a schema dump file, not only does NetBeans have to do less work to figure out your migrations, its format is predictable such that the code completion should be completely accurate.

P.S. This doesn't work right if you freeze Rails into your project; you need to be using Rails via Rubygems. I'll fix that soonish.


which release does this screenshot take from?
I am now playing with beta1, seems the ActiveRecord Completion does not work.

Posted by leondu on October 16, 2007 at 11:19 AM PDT #

@leondu, you will probably need to use a daily build from

Posted by Anthony Richardson on October 16, 2007 at 11:33 AM PDT #

Anthony is right - a daily would have this, as would beta2 (which is in high resistance right now - should release in a week or two)

Posted by Tor Norbye on October 16, 2007 at 11:36 AM PDT #

Way to go Tor. This is HUGE for beginners like myself.

Posted by Richard Poirier on October 16, 2007 at 02:37 PM PDT #

This very nice autocomplete doesn't work for me (I can post screenshots if there is a way to do so).
I have the latest nightly build (16/10/2007)
My connection to database in the server tab is ok and active (I can browse my database from netbeans)

Autocomplete does not provide interesting suggestion, like on your screenshots.

Other bug I have: rdoc doesn't show up for rails function, it shows only for ruby keywords and functions.

To be more precise: typing "ri validates_uniqueness_of" in a shell window will show me the doc for this rails function.
But hitting ctrl+shift+space when the cursor is on
"validates_uniqueness_of" in netbeans won't show anything.

Maybe this is due to my gem install path:
/var/lib/gems/1.8/ on a GNU/Linux ubuntu OS.

Any hints about what I can do to get all thoose amazing features working for me? Or maybe there is a better place to post this problem?

Posted by Sebastien on October 16, 2007 at 08:02 PM PDT #

Hi Sebastien,
these two issues are probably related. (Having a database connection doesn't matter, NetBeans doesn't use that for completion, it's instead inferring information from your migration files.)

I'd like to get more information; I have an Ubuntu machine too and it's working for me. I'm assuming you're using $GEM_ROOT to point to your gems.

The various feedback channels for bugs, ideas, etc. are listed here:

One more thing to try in case you're on Rails 1.2.5 and had been running some other NetBeans daily builds recently is to wipe out your userdir's "var/cache/" subdirectory and restart NetBeans.

Posted by Tor Norbye on October 17, 2007 at 12:05 AM PDT #

Great work, Tor! It's too bad this uses data from the migration to dig the details out for auto-complete, because if you use ActiveRecord for non-Rails Ruby projects, you don't get the goodness! Is there any plan to bring this same auto-complete power out of a Rails-only project base?

Posted by Warren Wessels on October 17, 2007 at 01:02 AM PDT #

My env variables are
export GEM_HOME=/var/lib/gems/1.8
export GEM_PATH=/var/lib/gems/1.8
export GEM_ROOT=/var/lib/gems/1.8

I'm running rails 1.2.5
deleted all the ".netbeansxx" folders in my home (from old version of netbeans I had)
deleted all the content of ".nbrubyide/dev/var/cache" in my home folder
relaunched netbeans

still get the same behaviour: no documentation neither autocompletion for rails function or named variables. (throug available with ri in the shell command line).
autocompletion is available for function that have template shortcut (i.e. "validates_associated" has a "va" shortcut, and is shown in autocompletion).

The inferring from migration files seems to work fine now in controller, but not in migration file
class AddPrice < ActiveRecord::Migration
def self.up
rename_column (<-autocompletion does not suggest ":products" here)

talking about cache:
Noticed I have this 2 files: /var/lib/gems/1.8/source_cache
~/.gem/source_cache (in my home).

should I delete them also?
Are my env variables wrong?

Will post an issue about this

Posted by Sebastien on October 17, 2007 at 01:38 AM PDT #


Two questions:

1. Does this work with Rails 2? It's not working for me but I have the Rails 2 preview gems installed.

2. Does it display the db fields in the navigator window? That'd be way cool.

Posted by Steve Molitor on October 17, 2007 at 02:28 AM PDT #

Hey Tor, what were the steps for importing TM bundles again? I could very much use the TextMate Liquid bundle at times and it is a shame that its not bundled by default since the heavy lifting has already been done.

Posted by ylon on October 17, 2007 at 11:52 AM PDT #

Hi Warren,
sorry - I don't know much about what non-Rails usage of ActiveRecord would look like. If you can file an issue with some more information (for example, is there the equivalent of schema.rb somewhere?) I can look at it.

Hi Sebastien,
thanks for filing the issue (#119240, right?). I'll follow up via the issuetracker.

Hi Steve,
sorry, it doesn't work with Rails 2 yet - for the same reason it doesn't work with edge Rails frozen into Rails apps. I will look at this really soon now (just finishing up some high priority fixes for the beta2 branch). There's no db fields in the navigator; the navigator shows the structure of the current file, not really inferred information like inherited methods or the database stuff. I plan to enhance this in the next release such that for example RSpec files show logical descriptions itemized there - something similar could be done at least for the migration and schema files if not the model files themselves.

Hi Ylon,
importing a bundle should be trivial; it uses an extra module. I've updated the version of a bit so I should probably do the migration myself or sync back my changes. I'll try to remember to look at this once I'm done with beta2 (a couple more days). Ping me if I forget, I'm juggling too many balls at the moment :)

Posted by Tor Norbye on October 18, 2007 at 05:54 AM PDT #


I'll look into it and post an issue. Knowing that you are parsing the schema.rb file helps... perhaps I can trick it into providing the info if I generate/place a schema.rb file in a similar location to the project root?

Posted by Warren Wessels on October 18, 2007 at 06:06 AM PDT #

Tor: issue #119240, That's right. Thanks for your reply. can't wait for getting thoose incredibles features working for me. You are making Netbeans a killer ap!
By the way: I'm trying to have netbeans using the same style and fonts as my other aps. Seems you resolved this point for you.
I set everything so my gtk aps (thunderbird, firefox, openoffice...) looks the same as my qt aps.
But Netbeans resists me.
Launching with --laf does only import the color model, the fontsize, but not the font family. I spent hours today trying to solve this. If you have any idea or documentation I may look at...

Posted by Sebastien on October 18, 2007 at 06:28 AM PDT #

Hi Sebastien,
Just making sure: Are you using at least Java 6 Update 1? There were a LOT of improvements in the GTK look and feel in 6 update 1. If you are running with at least 6 update 1, NetBeans will automatically use the GTKLookAndFeel - no --laf flag is necessary. If you are running on older JDKs you can force it the way you have shown but the reason NetBeans doesn't do it for you is that there are a lot of problems. (There are a lot of other good reasons to use Java 6 SE update 1 from the GUI perspective - font handling, gray rect fixes etc.)

Posted by Tor Norbye on October 18, 2007 at 06:33 AM PDT #

Hi Tor
Thanks for your answer. I managed to get the jdk update3 installed, and running by default.
This improved a little problem with the buttons permitting to navigate between the editors file.

But what you expected did not happen: the style (color and font size) is still not used automatically (style have to precise the --laf option to have netbeans import font styles and colors).

And the main problem is still there (font familly is not the same as in system)

I think I will report this as a bug, since other java-based apps manage to integrate ok (so I should not have to install more things to get this running).

Posted by Sebastien on October 18, 2007 at 07:14 PM PDT #


Just wanted to say that you're the man. I'm starting my first real Rails project this weekend and having great IDE support just makes working with Ruby and Rails even more enjoyable.

Thank you.

Posted by Chris on October 19, 2007 at 09:31 AM PDT #

I hope Gavin King sees this.

Posted by Brian Ehmann on October 22, 2007 at 12:59 AM PDT #

I've been using netbeans 6 beta 1 at work for all my Ruby on Rails development. I'm very impressed with it so far. Great job!

Posted by paul on October 22, 2007 at 05:17 AM PDT #

hi ,
thanks a lot for your effort , but please I need somthing...:

I am a new to rails , so need a clear detailed demo for how to connect native rails project in netbeans with external DB (MySQL , Oracle , Postgres .....)not Derby.

do we need a a JDBC driver for this task in netbeans ..??

please , make it clear

Posted by ahmed ramadan on October 22, 2007 at 07:44 AM PDT #

Hi Sebastien,
I'm not sure what could be happening - it -should- pick up the GTK look and feel automatically if you're running NB6 and JDK 6u3. (Have you checked in the About box that it actually found your right JDK when it launched NetBeans?). I'm running Ubuntu Gutsy Gibbon right here and the fonts and colors are correctly picking up my Compiz theme - it even switches the IDE colors live as I'm changing them in the Appearance Preferences. I wonder if this is a nbrubyide problem and not an NB6 problem (I'm running NB6). Can you try with the Ruby-only version of NB6 from here:

Hi Ahmed, there's nothing NetBeans specific about that; when you create a Rails project, choose for example MySQL, and NetBeans will automatically open database.yml for you. You now need to point to the right database name, user and password - and possibly machine (usually localhost). I'll forward your request to the docs/tutorials writers in case they either already have something or are wondering what people need.

Warren - I don't think having just a db/schema.rb file will be enough since I also apply code completion only in classes known to be ActiveRecord models. If you can file an issue with more details for any conventions that are common with ActiveRecord usage outside of Rails we may get better support for this in the next version!

Thanks Chris, Brian and Paul for your comments!

Posted by Tor Norbye on October 22, 2007 at 08:00 AM PDT #

By the way Steve, I'm still working on vendor/rails/ etc. support, it turns out to be a bit involved. (Plus I took some time out to improve the Go To Declaration interaction - there's now a popup when NetBeans is not sure -which- of the potentially many declarations of a class/method to jump to.)

Posted by Tor Norbye on October 22, 2007 at 08:02 AM PDT #

This is for Ahmed who asks for a demo on connecting a native Rails project to a non-derby database. We do not have a demo. However, we have 3 documents that specifically address how to connect Rails projects to various databases. Do any of these help answer your questions:

If these do not answer your questions, please use the Feedback button at the bottom of setting-up.html to give us more details about the missing information.



Posted by diva #2 on October 22, 2007 at 08:18 AM PDT #

@tor: Agree with Chris. I'm very impressed by your reactivity! Maybe is tor more than a single person? :-). I recommended you blog arround me.

The Ruby-only version of NB6 you pointed me has exactly the same behaviour concerning fonts and styles

About says (in the nbrubyide program):
Product Version: NetBeans Ruby IDE 20071022171851
Java: 1.6.0_03; Java HotSpot(TM) Client VM 1.6.0_03-b05
System: Linux version 2.6.20-16-generic running on i386; UTF-8; fr_FR (nbrubyide)
Userdir: /home/seb/.nbrubyide/dev

I noticed that the "netbeans" font is quite similar to the system one, so to get things clear, it's better to set system font to one quite strange (e.g: a font that looks like handwriting)

Anyway I begin to get used to the netbeans font, but I think a lot of people wont like this at first glance.

I have been active on gathering request about the ability to change the global netbeans font (this would be a workaround).

Still have to post a bug for this GTK behaviour, but strange that you don't have the same. I saw screenshot of netbeans with the same problem as mine, so it is possible to reproduce it. But I saw also screenshots of people without this problem, and that makes me nervous ;-) because I know there is a solution and I have not found it!

Posted by Sebastien on October 22, 2007 at 09:35 AM PDT #

Is there an estimated timeframe for compiling JRuby within the IDE? Thanks!

Posted by brian on October 23, 2007 at 01:49 AM PDT #

I finally committed a bunch of changes around indexing, gem management and the vendor/ directory. Thus, the active record completion -should- work with edge rails now. Steve, would you mind taking it for a spin? You'll need build 4866 or later from .

Brian, I believe JRuby bytecode compilation is slated for JRuby 1.1; I think a beta is not very far away but the jruby dev alias would be the best place to ask.

Sebastien - I don't know what to tell you regarding linux & the look and feel, but if you're not getting the GTK look and feel by default on a recent Java 6 update release then that's a significant bug - can you make sure that gets filed with all the relevant details?

Posted by Tor Norbye on October 26, 2007 at 07:25 AM PDT #

Post a Comment:
Comments are closed for this entry.

Tor Norbye


« July 2016