Ruby on Rails generates model field:type - what are the options for field:type?

Ruby on-RailsGeneratorRails Activerecord

Ruby on-Rails Problem Overview


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...

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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

See the table definitions section.

Solution 2 - Ruby on-Rails

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.

Solution 3 - Ruby on-Rails

$ 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

Solution 4 - Ruby on-Rails

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

Solution 5 - Ruby on-Rails

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.

Solution 6 - Ruby on-Rails

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.

Solution 7 - Ruby on-Rails

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

Solution 8 - Ruby on-Rails

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMeltemiView Question on Stackoverflow
Solution 1 - Ruby on-RailsPaul SchreiberView Answer on Stackoverflow
Solution 2 - Ruby on-RailsTroyView Answer on Stackoverflow
Solution 3 - Ruby on-RailsB SevenView Answer on Stackoverflow
Solution 4 - Ruby on-RailsRaghuView Answer on Stackoverflow
Solution 5 - Ruby on-RailsVictor AugustoView Answer on Stackoverflow
Solution 6 - Ruby on-Railsjustinedps26View Answer on Stackoverflow
Solution 7 - Ruby on-RailschandanjhaView Answer on Stackoverflow
Solution 8 - Ruby on-RailschandanjhaView Answer on Stackoverflow