322

I'm trying to generate a new model and forget the syntax for referencing another model's ID. I'd look it up myself, but I haven't figured out, among all my Ruby on Rails documentation links, how to find the definitive source.

$ rails g model Item name:string description:text (and here either reference:product or references:product). But the better question is where or how can I look for this kind of silliness easily in the future?

Note: I've learned the hard way that if I mistype one of these options and run my migration then Ruby on Rails will totally screw up my database... and rake db:rollback is powerless against such screwups. I'm sure I'm just not understanding something, but until I do... the "detailed" information returned by rails g model still leaves me scratching...

strivedi183
  • 4,749
  • 2
  • 31
  • 38
Meltemi
  • 37,979
  • 50
  • 195
  • 293
  • What about :uniq and :index field options? Like in "rails g model title body:text tracking_id:integer:uniq". I couldn't find documentation for those. Are there any more? – Kangur Feb 12 '13 at 13:24
  • 14
    `rails generate model --help` – Dennis Jan 21 '14 at 18:51
  • 1
    Using a version control would provide you with an easy way to rollback any generated files. And if the problem is in the database... well, you can always do db:schema:load – Hector Ordonez Mar 06 '15 at 15:59

8 Answers8

505
:primary_key, :string, :text, :integer, :float, :decimal, :datetime, :timestamp,
:time, :date, :binary, :boolean, :references

See the table definitions section.

Dennis
  • 56,821
  • 26
  • 143
  • 139
Paul Schreiber
  • 12,531
  • 4
  • 41
  • 63
  • 14
    hmm...no mention of `:reference` or `:references` in your answer *or* an explanation of how to pass it to generators in the link you provided?!? – Meltemi Dec 08 '10 at 18:51
  • Example: `rails generate model Song name:string mp3url:string description:text` --will make a model Song with properties name with type string, etc.. – That Realty Programmer Guy Mar 20 '11 at 22:34
  • 56
    That doesn't answer the question at all. – MikeEL Jun 01 '11 at 19:11
  • 1
    Selected this as the answer but know that **:references** is also an option. – Meltemi Jul 14 '11 at 23:06
  • useful link related to this answer: http://www.orthogonalthought.com/blog/index.php/2007/06/mysql-and-ruby-on-rails-datatypes/ – Jan Hettich Jun 10 '12 at 08:27
  • 14
    Is there some documentation that actually *defines* these column types? For instance, how does `string` differ from `text`? – Grant Birchmeier Aug 09 '12 at 17:32
  • http://guides.rubyonrails.org/migrations.html#creating-a-table The mappings to native database types aren't explicitly defined as that would have to be done for every database supported. However, a sensible mapping will occur. In the case of string and text, I believe it's varchar(255) and text respectively. The short answer is suck and see. Either look through the open source code or create a database with your chosen adaptor and examine the result using the command line or a visual tool. – dark fader Jan 21 '13 at 19:20
  • What about :uniq and :index field options? Like in "rails g model title body:text tracking_id:integer:uniq". I couldn't find documentation for those. Are there any more? – Kangur Feb 12 '13 at 13:25
  • 3
    @Kangur the `uniq` and `index` suffixes (and all the types) are documented in the usage of `rails generate model`. Run `rails g model` to see the usage docs. – Dennis Oct 01 '14 at 18:19
  • Another (not well documented) option is `belongs_to` as can be seen [here](https://coderwall.com/p/f5mdoq/using-belongs_to-with-scaffold-generator-to-quickly-create-associations). But I think it does the exact same as `references`. – EthanP Jan 26 '15 at 16:27
  • Unfortunate that @Meltemi's subject line and body of his question didn't match. Nevertheless, there are some helpful comments on this page, thanks. – MSC Feb 20 '15 at 23:07
  • Your link is unhelpful. – vcardillo Sep 19 '16 at 17:38
  • answered my question! – thedanotto Oct 12 '17 at 22:52
  • Just to mention that the list of available column types is now expanded to: :primary_key, :string, :text, :integer, :bigint, :float, :decimal, :numeric, :datetime, :time, :date, :binary, :boolean. (https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-add_column) – Graeme Campbell Mar 29 '20 at 11:32
  • :token is also now an option. – Tim Haines Jul 06 '21 at 23:52
192

To create a model that references another, use the Ruby on Rails model generator:

$ rails g model wheel car:references

That produces app/models/wheel.rb:

class Wheel < ActiveRecord::Base
  belongs_to :car
end

And adds the following migration:

class CreateWheels < ActiveRecord::Migration
  def self.up
    create_table :wheels do |t|
      t.references :car

      t.timestamps
    end
  end

  def self.down
    drop_table :wheels
  end
end

When you run the migration, the following will end up in your db/schema.rb:

$ rake db:migrate

create_table "wheels", :force => true do |t|
  t.integer  "car_id"
  t.datetime "created_at"
  t.datetime "updated_at"
end

As for documentation, a starting point for rails generators is Ruby on Rails: A Guide to The Rails Command Line which points you to API Documentation for more about available field types.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Troy
  • 5,319
  • 1
  • 35
  • 41
7

$ rails g model Item name:string description:text product:references

I too found the guides difficult to use. Easy to understand, but hard to find what I am looking for.

Also, I have temp projects that I run the rails generate commands on. Then once I get them working I run it on my real project.

Reference for the above code: http://guides.rubyonrails.org/getting_started.html#associating-models

B Seven
  • 44,484
  • 66
  • 240
  • 385
3

Remember to not capitalize your text when writing this command. For example:

Do write:

rails g model product title:string description:text image_url:string price:decimal

Do not write:

rails g Model product title:string description:text image_url:string price:decimal

At least it was a problem to me.

Barranka
  • 20,547
  • 13
  • 65
  • 83
Victor Augusto
  • 2,406
  • 24
  • 20
  • Wait?!? What? I capitalize my model names all the time! What "problems" are you seeing? – Meltemi Aug 22 '12 at 17:22
  • 2
    I'm not talking about the name of your model, but the name 'Model'. I tried to create a model like this: rails g Model product title:string and got: Could not find generator Model. so i tried it like this: rails g model product title:string and it worked. – Victor Augusto Sep 20 '12 at 14:09
  • 1
    Ah. Hadn't encountered that b4. Good tip! – Meltemi Sep 20 '12 at 14:25
  • 2
    (It's not the model name that's the issue, it's the word `model` as referring to the generator. `rails g model Product …` is fine.) – Asherah Jan 16 '14 at 01:41
3

http://guides.rubyonrails.org should be a good site if you're trying to get through the basic stuff in Ruby on Rails.

Here is a link to associate models while you generate them: http://guides.rubyonrails.org/getting_started.html#associating-models

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Raghu
  • 2,543
  • 1
  • 19
  • 24
0

I had the same issue, but my code was a little bit different.

def new
 @project = Project.new
end

And my form looked like this:

<%= form_for @project do |f| %>
     and so on....
<% end %>

That was totally correct, so I didn't know how to figure it out.

Finally, just adding

url: { projects: :create }

after

<%= form-for @project ...%>

worked for me.

Coding Enthusiast
  • 3,865
  • 1
  • 27
  • 50
justinedps26
  • 159
  • 1
  • 3
  • 15
  • This may be a good answer but I'm not sure how it relates to the OP which is about field types (`:integer`, `:string`, etc…). – Meltemi Oct 18 '16 at 19:04
0

It's very simple in ROR to create a model that references other.

rails g model Item name:string description:text product:references

This code will add 'product_id' column in the Item table

chandanjha
  • 891
  • 6
  • 5
0

There are lots of data types you can mention while creating model, some examples are:

:primary_key, :string, :text, :integer, :float, :decimal, :datetime, :timestamp, :time, :date, :binary, :boolean, :references

syntax:

field_type:data_type
Coding Enthusiast
  • 3,865
  • 1
  • 27
  • 50
chandanjha
  • 891
  • 6
  • 5