If you look in db/schema.rb you will see something like:
create_table "users", :force => true do |t|
What does the :force => true mean?
From the Rails docs:
:force
Set to true to drop the table before creating it. Defaults to false.
Basically, this helps ensure database integrity. If you're manually tooling around with your migrations, it's helpful to ensure that you're creating new tables from a clean slate, rather than risking naming conflicts that stem from tables that have been created on a one-off basis.
This simply drops the table before creation. Check the docs for more info here: ActiveRecord::ConnectionAdapters::SchemaStatements
Related
How do I add data to a table in Rails?
So far I have created a Rails app that pulls in data from an API. Next, I have ran the command
rails generate model order
and I have /db/migrate/timestamp_create_orders.rb
class CreateOrders < ActiveRecord::Migration
def change
create_table :orders do |t|
t.string :email, null: false
t.string :order_date, null: false
t.string :total_price, null: false
t.string :order_number, null: false
t.string :cust_name, null: false
t.string :zip_code, null: false
t.timestamps null: false
end
end
end
Next, I believe I need to run db:migrate and that will create the table.
My question is, how do I add data to this table? I want the user to visit the app, it pulls in the data, and stores it in this table.
I have found conflicting advice..
Should I just use the advice from here
Order.create(:email=>'fake#fake.com',:order_data=>"...
But other advise seems to say not to do this here and here. Though they are all pretty old
You do not create database entries in migrations, you usually create schema or specify changes in the schema in migration files. You use seeds for creating seed data in the database.
To create new data in database through rails you can use either create or new method but you need to save the data as mentioned in other posts in your links when you are using new method.
While creating or migrating a new database table, table row is not automatically added. You need to add them manually. One way to populate the newly created database table is using seeds.rb file which is located in your application db folder. You can add Faker gem to your application for creating fake table attribute elements. An example using faker:
(1..3).each do # it'll create 3 new order
Order.create(email: Faker::Internet.email, order_date: Faker::Date.between(2.days.ago, Date.today))
end
Then run rake db:seed in your project folder console.
If you have some validation in your order.rb file, then you can create new instance of that order and then save it like:
order = Order.new(....)
order.save(validate: false)
On my ruby on rails app i added following code in schema.rb file to create a new table in database
create_table "label_info", :force => true do |t|
t.text "body"
t.string "title"
t.integer "label_id"
end
and then run rake db:migrate command but nothing is happening. I thought it would create a new table in database .
If you read the first line of your schema.rb file you will see:
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
I would recommand to do rails g model label_info
May be delete :force => true, because since you haven't created the table it keeps to create first.
:force Set to true to drop the table before creating it. Defaults to false.
Either you have to destroy the model and recreate it or you have to change the migration number and rename the file, for the older schema version. You can use current time stamp. like 20150808114518_abc_xyz.rb
Thanks!
If I have a field and it already has had a migration to set :null => false and I then do another migration to add :unique => true - do I now need to list the existing :null => false also in the new migration?
If I do not list it in the new migration does that imply it actually gets removed?
all migration changes are relative to current database state, you don't need to list all existing changes from the beginning.
Good way to became sure is simply perform a test. gl
I have a link table which looks like this:
create_table "links", :id => false, :force => true do |t|
t.integer model1_id
t.integer model2_id
t.string someotherinfo
end
I'm currently defining routes like this:
match '/links/:model1_id/:model2_id/' => buggable_links#validate
It seems like I ought to be able to do something more like resources rather than writing out all the match statements. What's the right way of having rails generate resource routes on models which do not have a single primary key, such that URLs contain two IDs?
N.B. I'm aware that one possible answer is 'just add an autoincrementing pk'. The pros and cons of that are discussed in this question, but for the purposes of this question let's assume I want to leave my DB schema as it is.
This is the correct way to do it.
The only thing you should add is in the resources of buggable_links add the validate function as a get method.
I'm just figuring out my way around rails but I need a little help with the rails generate scaffold command.
Here's the command that I'd like to use
rails generate scaffold Expense user:??? name:string description:text
I'd like the description field to be nullable and the users field to be linked to another Model — in this case I'd like to create a foreign key to the Users. I'm using the devise authentication framework.
I've read that many RoR developers try and avoid the scaffolding method and opt for the manual approach instead but my web-app is quite simple and I've thought of going the scaffolding way.
Scaffolding only generates the migration that you then run. Once the file is generated simply crack open the generated migration and adjust any of the values you need specific constraints on. By default columns are set to null unless you specify otherwise e.g.:
create_table "slugs", :force => true do |t|
t.integer "sequence", :default => 1, :null => false
t.string "sluggable_type", :limit => 40
t.string "scope", :limit => 40
t.datetime "created_at"
end
This is the code generated by the friendly_id plugin as you can see they have specified that the sequence column cannot be null while the other fields have other constraints.