RoR: Cannot Migrate Database to Heroku - ruby-on-rails

I am having trouble migrating my database to Heroku. I have checked the other issues that address this to no avail. I can really use a second pair of eyes on my code to help me figure this out.
This is the error I get:
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::UndefinedTable: ERROR: relation "props" does not exist
: ALTER TABLE "comments" ADD CONSTRAINT "fk_rails_1d3f70cf04"
FOREIGN KEY ("prop_id")
REFERENCES "props" ("id")
It seems to get caught while migrating this file:
class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.string :commenter
t.text :body
t.references :prop, index: true, foreign_key: true
t.timestamps null: false
end
end
end
This is the migration file where I create the table props:
class CreateProps < ActiveRecord::Migration
def change
create_table :props do |t|
t.string :title
t.text :text
t.references :user, index: true, foreign_key: true
t.timestamps null: false
end
end
end
My schema is here:
ActiveRecord::Schema.define(version: 20160528205746) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "answers", force: :cascade do |t|
t.string "choice"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "created_by"
t.integer "user_id"
t.integer "prop_id"
end
create_table "comments", force: :cascade do |t|
t.string "commenter"
t.text "body"
t.integer "prop_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "comments", ["prop_id"], name: "index_comments_on_prop_id", using: :btree
create_table "props", force: :cascade do |t|
t.string "title"
t.text "text"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "choice"
t.string "answer"
t.integer "answerId"
end
add_index "props", ["user_id"], name: "index_props_on_user_id", using: :btree
create_table "user_answers", force: :cascade do |t|
t.integer "user_id"
t.integer "answer_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "users", force: :cascade do |t|
t.string "username"
t.string "email"
t.integer "score", default: 0
t.integer "prop_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
t.string "created_by"
t.boolean "admin", default: false
t.integer "answers_id"
t.integer "answer_id"
end
add_index "users", ["answer_id"], name: "index_users_on_answer_id", using: :btree
add_index "users", ["prop_id"], name: "index_users_on_prop_id", using: :btree
create_table "wins", force: :cascade do |t|
t.string "correctAnswer"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "wins", ["user_id"], name: "index_wins_on_user_id", using: :btree
add_foreign_key "users", "answers"
end

The problem is you are creating a reference to a table that is not yet created. Remove the reference from that migration to props, then add the props table and then add a migration implementing the association. If you dont need the data currently in the db I would do a "rake db:drop" and edit the migration files (only if you arent collaborating with others!)
Update:
Do rails g migration add_ref_to_comments
Then edit the migration to have:
def change
add_reference :props, :comment, index: true
end

Related

Rails: how to create a migration that add 'using: :btree' to foreign key

I have two models
father has_many sons
son belongs_to father
Normally, this piece of code
def change
add_reference :sons, :father, index: true
end
it will generate
add_index "sons", ["father_id"], name: "index_sons_on_father_id"
Now, I want it generate
add_index "sons", ["father_id"], name: "index_sons_on_father_id", using: :btree
How to write the migration?
you just check this
you can do look like this.
ActiveRecord::Schema.define(version: 20150321171548) do
create_table "authors", force: :cascade do |t|
t.string "name"
t.string "hashtags"
t.string "avatar"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "articles", force: :cascade do |t|
t.integer "author_id", limit: 4
t.string "title", limit: 255
t.string "bannerurl", limit: 255
t.string "thumbnailurl", limit: 255
t.text "content", limit: 65535
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "articles", ["author_id"], name: "index_articles_on_author_id", using: :btree
end

rails eager loading for one-to-many association

I have
class Article < ActiveRecord::Base
has_one :template
end
and
class Template < ActiveRecord::Base
has_many :articles
end
where Template model has got a room property. Now i'd like to build a list of all articles, where the articles template has a certain room value (say "bath").
I thought this is done by eager loading (resp: includes), but if i try
Article.includes(:template)
I get the error
SELECT "templates".* FROM "templates" WHERE "templates"."article_id" IN ('51', '52', '53', '54')
ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column templates.article_id does not exist
LINE 1: SELECT "templates".* FROM "templates" WHERE "templates"."art...
What am I doing wrong?
EDIT
here's my schema.rb as asked
# encoding: UTF-8
# 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.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20160913122551) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "articles", force: :cascade do |t|
t.string "title"
t.text "details"
t.integer "value_eur"
t.integer "deposit_eur"
t.integer "location_id"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "picture"
t.float "rate_eur"
t.string "rate_interval"
t.integer "template_id"
t.integer "quality"
end
add_index "articles", ["location_id"], name: "index_articles_on_location_id", using: :btree
add_index "articles", ["template_id"], name: "index_articles_on_template_id", using: :btree
add_index "articles", ["user_id"], name: "index_articles_on_user_id", using: :btree
create_table "locations", force: :cascade do |t|
t.string "street_and_no"
t.string "postcode"
t.string "city"
t.string "country"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.float "latitude"
t.float "longitude"
end
add_index "locations", ["user_id"], name: "index_locations_on_user_id", using: :btree
create_table "templates", force: :cascade do |t|
t.string "title"
t.text "details_hint"
t.float "rate_eur"
t.string "rate_interval"
t.string "picture"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "room"
end
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.inet "current_sign_in_ip"
t.inet "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "role"
t.string "nickname"
t.string "firstname"
t.string "lastname"
t.string "phoneno"
t.boolean "showemail"
t.boolean "showphone"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
add_index "users", ["nickname"], name: "index_users_on_nickname", unique: true, using: :btree
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
add_foreign_key "articles", "locations"
add_foreign_key "articles", "templates"
add_foreign_key "articles", "users"
add_foreign_key "locations", "users"
end
Your templates table does not have an article_id column according to the schema.rb you posted so you will need to create that reference.
Change
has_one :template
in the articles model to
belongs_to :template

Rails: Sqlite3 to PostgreSQL heroku deployment Undefined Table ERROR

I've built a rails app and on pushing it up to heroku and running heroku run rake db:migrate i receive the error:
Migrating to CreateFollowingRelationships (20160615113231)
(0.4ms) BEGIN
== 20160615113231 CreateFollowingRelationships: migrating =====================
-- create_table(:following_relationships)
(7.1ms) CREATE TABLE "following_relationships" ("id" serial primary key, "follower_id" integer, "followed_user_id" integer, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL, CONSTRAINT "fk_rails_39102b381e"
FOREIGN KEY ("follower_id")
REFERENCES "followers" ("id")
, CONSTRAINT "fk_rails_048c8f7cd9"
FOREIGN KEY ("followed_user_id")
REFERENCES "followed_users" ("id")
)
(0.5ms) ROLLBACK
(0.5ms) SELECT pg_advisory_unlock(2837140123622957145)
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::UndefinedTable: ERROR: relation "followers" does not exist
: CREATE TABLE "following_relationships" ("id" serial primary key, "follower_id" integer, "followed_user_id" integer, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL, CONSTRAINT "fk_rails_39102b381e"
FOREIGN KEY ("follower_id")
REFERENCES "followers" ("id")
, CONSTRAINT "fk_rails_048c8f7cd9"
FOREIGN KEY ("followed_user_id")
REFERENCES "followed_users" ("id")
)
The relationship I have setup is for users to follow users and be followed. It works perfectly in development. This is the first time i've had this error and can't find a solution. Please see the rest of my code below.
db/schema.rb
ActiveRecord::Schema.define(version: 20160618122126) do
create_table "chats", force: :cascade do |t|
t.integer "user_id"
t.string "title"
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "profile_id"
t.index ["profile_id"], name: "index_chats_on_profile_id"
t.index ["user_id"], name: "index_chats_on_user_id"
end
create_table "comments", force: :cascade do |t|
t.integer "profile_id"
t.integer "chat_id"
t.text "body"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["chat_id"], name: "index_comments_on_chat_id"
t.index ["profile_id"], name: "index_comments_on_profile_id"
end
create_table "following_relationships", force: :cascade do |t|
t.integer "follower_id"
t.integer "followed_user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["followed_user_id"], name: "index_following_relationships_on_followed_user_id"
t.index ["follower_id"], name: "index_following_relationships_on_follower_id"
end
create_table "locations", force: :cascade do |t|
t.string "locationable_type"
t.integer "locationable_id"
t.string "suburb"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "address"
t.string "state"
t.string "country"
t.string "postcode"
t.float "latitude"
t.float "longitude"
t.index ["locationable_type", "locationable_id"], name: "index_locations_on_locationable_type_and_locationable_id"
t.index ["user_id"], name: "index_locations_on_user_id"
end
create_table "orders", force: :cascade do |t|
t.integer "buyer_id"
t.integer "seller_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "posts", force: :cascade do |t|
t.integer "profile_id"
t.integer "user_id"
t.string "title"
t.text "body"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["profile_id"], name: "index_posts_on_profile_id"
t.index ["user_id"], name: "index_posts_on_user_id"
end
create_table "profiles", force: :cascade do |t|
t.integer "user_id"
t.text "bio"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["user_id"], name: "index_profiles_on_user_id"
end
create_table "tutoring_relationships", force: :cascade do |t|
t.integer "tutor_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "tutee_id"
t.index ["tutee_id"], name: "index_tutoring_relationships_on_tutee_id"
t.index ["tutor_id"], name: "index_tutoring_relationships_on_tutor_id"
end
create_table "users", force: :cascade do |t|
t.string "email", null: false
t.string "password_digest", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "username"
t.boolean "tutor", default: false
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["username"], name: "index_users_on_username", unique: true
end
create_table "votes", force: :cascade do |t|
t.string "votable_type"
t.integer "votable_id"
t.string "voter_type"
t.integer "voter_id"
t.boolean "vote_flag"
t.string "vote_scope"
t.integer "vote_weight"
t.datetime "created_at"
t.datetime "updated_at"
t.index ["votable_id", "votable_type", "vote_scope"], name: "index_votes_on_votable_id_and_votable_type_and_vote_scope"
t.index ["voter_id", "voter_type", "vote_scope"], name: "index_votes_on_voter_id_and_voter_type_and_vote_scope"
end
end
models/following_relationship.rb
class FollowingRelationship < ApplicationRecord
belongs_to :follower, class_name: 'User'
belongs_to :followed_user, class_name: 'User'
end
models/user.rb
class User < ActiveRecord::Base
has_one :profile, dependent: :destroy
has_many :chats
validates :email, presence: true, uniqueness: true
validates :password_digest, presence: true
has_many :follower_relationships,
foreign_key: :followed_user_id,
class_name: 'FollowingRelationship'
has_many :followers, through: :follower_relationships
has_many :followed_user_relationships,
foreign_key: :follower_id,
class_name: "FollowingRelationship"
has_many :followed_users, through: :followed_user_relationships
has_many :tutee_relationships,
foreign_key: :tutor_id,
class_name: 'TutoringRelationship'
has_many :tutees, through: :tutee_relationships
has_many :tutor_relationships,
foreign_key: :tutee_id,
class_name: 'TutoringRelationship'
has_many :tutors, through: :tutor_relationships
def following? user
followed_user_ids.include?(user.id)
end
def is_connected? user
tutee_ids.include?(user.id)
end
end
Please excuse the messy model, it would usually be in a helper method but for this post it's here. If anyone has any ideas please let me know! Thanks

Heroku no implicit conversion of Symbol into Integer Error

I get the error with: heroku run rake db:migrate
Migrating to CreateResults (20150306174427)
== 20150306174427 CreateResults: migrating ====================================
-- create_table(:results)
-> 0.0479s
-- add_foreign_key(:results, :quantifieds, :users)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
no implicit conversion of Symbol into Integer/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.0.rc3/lib/active_record/connection_adapters/abstract/schema_statements.rb:707:in `[]'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.0.rc3/lib/active_record/connection_adapters/abstract/schema_statements.rb:707:in `add_foreign_key'
I can't understand it because run rake db:migrate works.
class CreateResults < ActiveRecord::Migration
def change
create_table :results do |t|
t.string :result_value
t.date :date_value
t.belongs_to :quantified, index: true
t.references :user, index: true
t.timestamps null: false
end
add_foreign_key :results, :quantifieds, :users
add_index :results, [:user_id, :created_at]
end
end
class CreateQuantifieds < ActiveRecord::Migration
def change
create_table :quantifieds do |t|
t.string :categories
t.string :metric
t.references :user, index: true
t.timestamps null: false
end
add_foreign_key :quantifieds, :users
add_index :quantifieds, [:user_id, :created_at]
end
end
# encoding: UTF-8
# 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.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20150306174427) do
create_table "days", force: true do |t|
t.integer "level_id"
t.integer "habit_id"
t.boolean "missed", default: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "goals", force: true do |t|
t.string "name"
t.date "deadline"
t.boolean "accomplished"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "goals", ["user_id", "created_at"], name: "index_goals_on_user_id_and_created_at"
add_index "goals", ["user_id"], name: "index_goals_on_user_id"
create_table "habits", force: true do |t|
t.datetime "left"
t.integer "level"
t.text "committed"
t.datetime "date_started"
t.string "trigger"
t.string "target"
t.string "reward"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "habits", ["user_id", "created_at"], name: "index_habits_on_user_id_and_created_at"
add_index "habits", ["user_id"], name: "index_habits_on_user_id"
create_table "levels", force: true do |t|
t.integer "user_id"
t.integer "habit_id"
t.integer "days_needed"
t.boolean "passed", default: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "quantifieds", force: true do |t|
t.string "categories"
t.string "metric"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "quantifieds", ["user_id", "created_at"], name: "index_quantifieds_on_user_id_and_created_at"
add_index "quantifieds", ["user_id"], name: "index_quantifieds_on_user_id"
create_table "relationships", force: true do |t|
t.integer "follower_id"
t.integer "followed_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "relationships", ["followed_id"], name: "index_relationships_on_followed_id"
add_index "relationships", ["follower_id", "followed_id"], name: "index_relationships_on_follower_id_and_followed_id", unique: true
add_index "relationships", ["follower_id"], name: "index_relationships_on_follower_id"
create_table "results", force: true do |t|
t.string "result_value"
t.date "date_value"
t.integer "quantified_id"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "results", ["quantified_id"], name: "index_results_on_quantified_id"
add_index "results", ["user_id", "created_at"], name: "index_results_on_user_id_and_created_at"
add_index "results", ["user_id"], name: "index_results_on_user_id"
create_table "taggings", force: true do |t|
t.integer "tag_id"
t.integer "taggable_id"
t.string "taggable_type"
t.integer "tagger_id"
t.string "tagger_type"
t.string "context", limit: 128
t.datetime "created_at"
end
add_index "taggings", ["tag_id", "taggable_id", "taggable_type", "context", "tagger_id", "tagger_type"], name: "taggings_idx", unique: true
add_index "taggings", ["taggable_id", "taggable_type", "context"], name: "index_taggings_on_taggable_id_and_taggable_type_and_context"
create_table "tags", force: true do |t|
t.string "name"
t.integer "taggings_count", default: 0
end
add_index "tags", ["name"], name: "index_tags_on_name", unique: true
create_table "users", force: true do |t|
t.string "name"
t.string "email"
t.text "missed_days"
t.text "missed_levels"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
t.string "remember_digest"
t.boolean "admin", default: false
t.string "activation_digest"
t.boolean "activated", default: false
t.datetime "activated_at"
t.string "reset_digest"
t.datetime "reset_sent_at"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
create_table "valuations", force: true do |t|
t.string "name"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "valuations", ["user_id", "created_at"], name: "index_valuations_on_user_id_and_created_at"
add_index "valuations", ["user_id"], name: "index_valuations_on_user_id"
end
UPDATE
Making these changes worked:
$ heroku pg:reset DATABASE
$ heroku rake db:migrate
class CreateResults < ActiveRecord::Migration
def change
create_table :results do |t|
t.string :result_value
t.date :date_value
t.integer :quantified_id
t.timestamps null: false
end
end
end
Thanks so much for your help! And for another perplexing question check this out: https://stackoverflow.com/questions/28863999/how-to-automate-into-method
t.belongs_to
Is invalid, you want either references or change it to integer and add _id to the field name. Then on your index add square brackets round the second and third arguments.

Rails Confused with refrential integrity

The more i read about foreign keys in rails i am getting more and more confused. In a post i read that its sufficient to add belongs_to and has_many/has_one in respective model file to getting things done. But again in another post I read that the index should be added to reference another table. Suppose There is writers table and book table in dbms while creating Books table we have to add
FOREIGN KEY (writers_Id) REFERENCES Writers(Id)
but in rails we in writer model we add has_many :book an in book model we add belongs_to :writer is both are equivalent ?
If both are equivalent then why we add index such as
add_index :books, :writer_id
I have project on which I am working on it has users has one personal information, academic information, application and rank. Also there is subject_streams which have streams and streams have cutoffs. Finally there is category which is independent. I dont know if i modeled data correctly but in the schema.rb am I doing correctly what i have said ?
ActiveRecord::Schema.define(version: 20140713133617) do
create_table "academics", force: true do |t|
t.integer "user_id"
t.integer "tenth_roll", default: 0
t.integer "tenth_year_pass", default: 2000
t.decimal "tenth_marks_percent", precision: 10, scale: 2, default: 40.0
t.string "tenth_board"
t.integer "hs_roll", default: 0
t.integer "hs_year_pass", default: 2002
t.decimal "hs_marks_percent", precision: 10, scale: 2, default: 40.0
t.string "hs_board"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "calculated_marks"
t.string "sub1"
t.integer "sub1_marks"
t.string "sub2"
t.integer "sub2_marks"
t.string "sub3"
t.integer "sub3_marks"
t.string "sub4"
t.integer "sub4_marks"
t.string "sub5"
t.integer "sub5_marks"
t.string "sub6"
t.integer "sub6_marks"
t.string "sub7"
t.integer "sub7_marks"
t.string "sub8"
t.integer "sub8_marks"
t.string "sub9"
t.integer "sub9_marks"
t.string "sub10"
t.integer "sub10_marks"
t.integer "subject_streams_id"
end
add_index "academics", ["user_id"], name: "index_academics_on_user_id", unique: true, using: :btree
create_table "applications", force: true do |t|
t.integer "user_id"
t.integer "stream_id"
t.datetime "created_at"
t.datetime "updated_at"
t.boolean "verified", default: false
end
add_index "applications", ["user_id"], name: "index_applications_on_user_id", unique: true, using: :btree
create_table "categories", force: true do |t|
t.string "category"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "cutoffs", force: true do |t|
t.integer "stream_id"
t.integer "category_id"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "cutoff_marks"
end
add_index "cutoffs", ["stream_id"], name: "index_cutoffs_on_stream_id", using: :btree
create_table "personals", force: true do |t|
t.integer "user_id"
t.date "date_of_birth"
t.string "gender"
t.string "blood_group"
t.string "fathers_name"
t.string "mothers_name"
t.text "address_present"
t.datetime "created_at"
t.datetime "updated_at"
t.string "first_name"
t.string "middle_name"
t.string "last_name"
t.integer "category_id"
t.string "image"
t.string "avatar"
end
add_index "personals", ["user_id"], name: "index_personals_on_user_id", unique: true, using: :btree
create_table "ranks", force: true do |t|
t.integer "user_id"
t.integer "rank"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "ranks", ["user_id"], name: "index_ranks_on_user_id", unique: true, using: :btree
create_table "registers", force: true do |t|
t.boolean "active"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "simple_captcha_data", force: true do |t|
t.string "key", limit: 40
t.string "value", limit: 6
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "simple_captcha_data", ["key"], name: "idx_key", using: :btree
create_table "streams", force: true do |t|
t.string "stream"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "seats"
t.integer "subject_stream_id"
end
add_index "streams", ["subject_stream_id"], name: "index_streams_on_subject_stream_id", using: :btree
create_table "subject_streams", force: true do |t|
t.string "subject_stream"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "users", force: true do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "level", default: 1
end
add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
end
In Ruby on Rails, the code has_many :books and belongs_to :writer is like defining relationships in the code level. It doesnt achieve anything in the database level. For example, if we write belongs_to :writer in the Book model, it just means that if there is a column writer_id in the books table, we can code something like this:
b = Book.first
b_writer = b.writer
#### equivalent to
# b_writer = Writer.find(b.writer_id)
# b_writer = Writer.where(:id => b.writer_id).first
# or b_writer = Writer.find_by_sql("SELECT writers.* from writers where writers.id = #{b.writer_id}")
It assumes that the foreign key is writer_id in books table by convention. If we need to change the foreign key for the queries generated by association, we need to specify it separately:
belongs_to :writer, :foreign_key => 'writerID'
So, in general declaring associations in models, gives us some utility methods to query those associations from database, instead of creating the queries manually. So, all the database related changes needs to be done in the migrations, like adding the column writer_id, adding index for column writer_id, setting writer_id with forieign key constraint etc. Rails does not support foreign_key constraint in migrations by default, as different databases handle foreign_key differently. For PostGre and MySQL database, you can use foreigner gem for adding foreign_key constraint.
Please read more about Rails associations

Resources