Question

I am a Java developer I've been learning Rails for the past few days. I have a Java EE application (Uses Hibernate for ORM) that I am trying to port to Rails. I have used scaffolding to generate a few of my models. But I have other models which contain references to other models. How do I define the relations? Can I scaffold that as well?

Here is an example for what I am trying to do.

public class Engine {
    private int valves;
    private int capacity;
    private int rpm;
}

I can scaffold the Engine class in ruby just by doing the following:

rails generate scaffold Engine valves:integer capacity:integer rpm:integer

Here is the tricky part for me:

public class Car {
    private Engine engine;
}

How do I scaffold the Car class in Ruby?

Was it helpful?

Solution

If I understand correctly you are looking for associations. Here's a great guide that you should read. The thing to understand here is that you define in your models how the they relate to each other with a series of methods described in that guide.

Here is what I would suggest you do:

rails generate scaffold Car <db columns>
rails generate model Engine valves:integer capacity:integer rpm:integer car_id:integer

In your two models:

class Car < ActiveRecord::Base
  has_one :engine
end 

class Engine < ActiveRecord::Base
  belongs_to :car
end

You can actually generate scaffold for both models...that will create controller and views. But in this case it might make sense to add

accepts_nested_attribues_for :engine

to your Car model instead. This will allow you to manage the manipulation of the Engine model from the controller and views of the Car model.

At any rate, I hope this helps you start to find what you need.

OTHER TIPS

You can do it using the references helper of activerecord migration.

rails generate scaffold Car engine:references ...

it will add :

t.references :engine in your migration file

has_many :engines in your Car model file

belongs_to :car in your Engine model file

Don't forget to check the rails api for options (default, relation callbacks...)(here for exemple : http://railsapi.com/doc/rails-v3.0.8rc1/)

You should learn more about Ruby. Ruby is not a static language, meaning every variable can hold every kind of object.

The rails generate command uses valves:integer etc. only for database purposes, because databases need this information.

Concerning your relations problem you should read about has_many, bleongs_to etc. (see http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html) In Rails you would define your relation like this

class Car
  belongs_to :engine
end

class Engine
  has_many :cars
end

Furthermore you have to add a foreign key engine_id to Car.

This works because there are several conventions used in Rails.

Without a basic tutorial you will not get far.

There is no scaffolding for relations, you have to learn how to do it "by hand" (which is not too demanding). Have a look at the "Rails Guides", and here "Active Record Association".

In your example, you have to do the following steps:

  1. Create a migration to migrate the database: rails g migration AddIds
  2. Modify the migration to include the additional ID you have to have:

    ...
    add_column :engines, :car_id, :integer
    
  3. Add to you models the following code:

    class Car
      has_one :engine
      ...
    end
    class Engine
      belongs_to :car
      ...
    end
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top