Showing posts with label ruby. Show all posts
Showing posts with label ruby. Show all posts

Tuesday, 6 September 2011

First steps with Rails: Inheritance

So I went back to my Rails code trying to figure out how to do the inheritance with scaffold.

Turns out... you can't do it :S (or at least I couldn't find an article on how to do it and all examples show a different path). You can specify the --parent attribute but it won't create the necessary migrations and the auto-generated CRUD pages will only contain the new fields.
So what I did was this:
I run

rails g scaffold TwitterUser --parent=User

That creates the class TwitterUser<User and the htmls for it. After that I had to manually create a migration. Rails uses STI out of the box, so I used that :P

My new migration had to add a column type of type string you have to use that name so rails will set the subclass name automatically for you.

After that you can add your custom columns.

class AddAndRemoveTwitterUserColumnsToUser < ActiveRecord::Migration
  def up
    add_column :users, :twitterName, :string
    add_column :users, :twitter_Id, :integer
    add_column :users, :type, :string
  end
  def down
    remove_column :users, :twitterName
    remove_column :users, :twitter_Id
    remove_column :users, :type
  end
end

After that you can go to your app/view and change the html and support the new fields. It's not that hard but I thought that scaffold tool could have done the work for me.


Monday, 29 August 2011

First impressions of Rails (and first entry)

I've been a Java developer for the last 8+ years. I worked with some other languages: C++, Fortran (yes... I know... ), javaScript. But most of my work was done in Java.

I recently decided that I want to try Ruby. I have a new project in mind, and everybody talks about how fast developing with Rails/Grails is, so I wanted to give it a chance.

What do you do when you change from one language to another? get a reference book? Well...not what I did :D It happens that my brother has been working with Rails for more than a year so instead of getting a book I'm trying to do it the hard way: reading blogs and asking my bro. whenever I find something weird.

OK so time to say what I think of Rails right now. Remember, just 1 day working with it (4/5hours actually):

I REALLY like Cucumber. It's great that you can specify your test in a human readable way, I don't have a Functional Analyst in my project, but I think you could use cucumber to let nontechnical people to write some test cases for you. Someone told me today that there are frameworks in java for doing the same thing: JBehave (Looks like I haven't done any full app in a while :D ). Anyway, important thing to notice: these are integration tests, you still need to create your unit tests.

I like how Rails organizes your app, it basically forces you to use good programming practices(thumbs up). Scripts are great you can generate your model classes, the db schema and all simple forms for your CRUD operations

What I DON'T Like is that you have to think in terms of the db first. I like to think in terms of objects, I don't want to pay attention to the DB schema when I'm writing my model.

Here's an example: Suppose you want to create a class SpecialUser with some attributes... let's say: name and twitterId.

rails g scaffold SpecialUser name:string twitterId:string

this works great, in order to make it work you run
rake db:migrate

and you are ready to go.

Now let's say we have a class User with the attribute name and we want this to be a subclass, so if you read the doc you'll find that you can specify the parent class with --parent so you run the script:

rails g scaffold SpecialUser twitterId:string --parent=User

You go an check the code and find

class SpecialUser < User
end

So you're like: "mmmmmmm this is weird... where's my twitterId attribute?" moreover, you check db/migration and there's no script for this new class... but if you do a grep by twitterId you'll find that the html pages were created.

So: looks like IF you want to have inheritance Rails will do PART of the job but you will have to write the db and model by yourself. I'm wondering how hard would have been to make it out of the box.

Next step: find out if there's a simple way to do inheritance or I have to write the whole thing by myself.