my app has 3 models, defined as follow:
class User < ActiveRecord::Base
has_many :vehicles, dependent: :destroy
has_one :insurance, through: :vehicle
end
class Vehicle < ActiveRecord::Base
belongs_to :user
has_one :insurance, dependent: :destroy
end
class Insurance < ActiveRecord::Base
belongs_to :vehicle
end
The resulting migration does not set any foreign keys for my insurances table. I expected to have two foreign keys, something like user_id and vehicle_id.
The resulting schema.rb file looks like this:
ActiveRecord::Schema.define(version: 20160314141604) do
create_table "insurances", force: :cascade do |t|
t.string "name"
t.date "issue_date"
t.date "expiry_date"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
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.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "unconfirmed_email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
create_table "vehicles", force: :cascade do |t|
t.text "name"
t.date "matriculation_date"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "vehicles", ["user_id"], name: "index_vehicles_on_user_id"
end
Why insurances table has no foreign keys? Thank you
Run the following migrations:
rails g migration AddUserIDToInsurances user:references
rails g migration AddVehicleIDToInsurances vehicle:references
Then run rake db:migrate. This should add the two foreign keys you mentioned to your insurances table.
You have to specifically set the association keys in a migration. If you create a new migration and add:
add_column :vehicles, :user_id, :integer
add_column :insurances, :user_id, :integer
add_index :vehicles, :user_id
add_index :insurances, :user_id
# or whatever columns and indexes you need...
Rails gives you the has_one has_many and belongs_to methods to associate models conveniently with ActiveRecord, but the keys are not auto-generated unless you deliberately configure them in a migration file.
Related
migrate file exists but no model for rails application.There are user and book model.I created join table between user and book model.
I write console : rails g migration CreateJoinTableBooksUsers books users
rake:db migrate
**schema.rb**
create_table "books", force: :cascade do |t|
t.string "title"
t.string "author"
t.integer "page_count"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.boolean "status"
t.string "user_id"
t.boolean "barter_status"
end
create_table "books_users", id: false, force: :cascade do |t|
t.bigint "book_id", null: false
t.bigint "user_id", null: false
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.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.string "username"
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
t.index ["username"], name: "index_users_on_username", unique: true
end
add_foreign_key "comments", "books"
add_foreign_key "comments", "users"
end
**migrate**
class CreateJoinTableBooksUsers < ActiveRecord::Migration[6.0]
def change
create_join_table :books, :users do |t|
t.index [:book_id, :user_id]
t.index [:user_id, :book_id]
end
end
end
A migration creates the tables in the database but doesn't create anything else.
But, for a true join table, you don't need a model:
https://guides.rubyonrails.org/association_basics.html#the-has-and-belongs-to-many-association
# app/models/user.rb
class User < ApplicationRecord
has_and_belongs_to_many :books
end
# app/models/books.rb
class Book < ApplicationRecord
has_and_belongs_to_many :users
end
IF you need scopes, callbacks, or methods on BooksUsers, you can use the has_many :through option:
https://guides.rubyonrails.org/association_basics.html#choosing-between-has-many-through-and-has-and-belongs-to-many
# app/models/user.rb
class User < ApplicationRecord
has_many :books_users
has_many :books, through: :books_users
end
# app/models/books.rb
class Book < ApplicationRecord
has_many :books_users
has_many :users, through: :books_users
end
In this case, you'll need to generate a model:
rails generate model BooksUsers
I created the following Active Record Schema using migrations but the relationships don't correspond to the schema. I've tried resetting, dropping, creating and migrating but in Rails C if i create a User u.User.create!(...), and then query u.groups or u.genres I get 'undefined method'
Thanks for your help
ActiveRecord::Schema.define(version: 20180603211047) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "genres", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "tag"
t.bigint "user_id"
t.index ["user_id"], name: "index_genres_on_user_id"
end
create_table "genres_users", id: false, force: :cascade do |t|
t.bigint "user_id", null: false
t.bigint "genre_id", null: false
end
create_table "groups", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "name"
t.bigint "user_id"
t.index ["user_id"], name: "index_groups_on_user_id"
end
create_table "groups_users", id: false, force: :cascade do |t|
t.bigint "user_id", null: false
t.bigint "group_id", null: false
end
create_table "playlists", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "name"
t.string "link"
t.text "description"
t.bigint "group_id"
t.index ["group_id"], name: "index_playlists_on_group_id"
end
create_table "users", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
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.string "name"
t.string "token"
t.date "birthday"
t.string "link"
t.string "playlistId"
t.string "country"
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
add_foreign_key "genres", "users"
add_foreign_key "groups", "users"
add_foreign_key "playlists", "groups"
end
here are the models:
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
#before_action :authenticate_user!
has_and_belongs_to_many :genres, :through => :genres_users
has_and_belongs_to_many :groups, :through => :groups_users
include Enumerable
end
class Genre < ApplicationRecord
has_and_belongs_to_many :users, :through => :genres_users
end
class Group < ApplicationRecord
has_and_belongs_to_many :users, :through => :groups_users
has_one :playlist
end
class Playlist < ApplicationRecord
belongs_to :group
end
The relationship is that Groups have users, users have genres (favourite genres!), these are has and belongs to relationships through join tables (multiple genres per user and multiple groups per user). Every group has a playlist, and there will be multiple playlists
[Edited after clarification from OP]
The relationship is that Groups have users, users have genres (favourite genres!), these are has and belongs to relationships through join tables (multiple genres per user and multiple groups per user). Every group has a playlist, and there will be multiple playlists
First off, you don't need a user_id column on groups or genres as that's not how the setup should work.
class Genre < ApplicationRecord
has_many :favorite_genres
has_many :users, through: :favorite_genres
[... other stuff]
end
class User < ApplicationRecord
has_many :group_memberships
has_many :groups, through: :group_memberships
has_many :favorite_genres
has_many :users, through: :favorite_genres
[... other stuff]
end
class Group < ApplicationRecord
has_many :group_memberships
has_many :users, through: :group_memberships
has_many :playlists
[... other stuff]
end
class Playlist < ApplicationRecord
belongs_to :group
end
class GroupMemberships < ApplicationRecord
belongs_to :user
belongs_to :group
[... other stuff]
end
class FavoriteGenres < ApplicationRecord
belongs_to :user
belongs_to :genre
[... other stuff]
end
So you'd drop the user_id column in groups. The connection happens in :group_memberships (the table formerly known as users_groups), which is a user_id, a group_id, and then you can have additional metadata columns as you need them (e.g. admin boolean/role, etc)
. This is called a "Has Many Through" relationship (http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association)
Likewise, a user's favorite genres is setup with a through relationship. So you'll have a separate database table AND model file for those through joins.
I don't think you need your add_foreign_key calls at all at this level, nor many of your indexes. You'll probably do more eager loading or possibly add indexes on the thorugh join tables and you'd do those like this in the schema:
t.index ["user_id", "genre_id"], name: "index_favorite_genres_on_user_id_and_genre_id"
Remember that belongs_to now creates a validation for that to be present in 5.x. You can override this by adding optional: true on that line in the model, e.g. belongs_to :foo, optional: true
So all that being said, here's your new schema:
create_table "genres", id: :serial, force: :cascade do |t|
t.string "tag"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "groups", id: :serial, force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "favorite_genres", id: false, force: :cascade do |t|
t.bigint "user_id", null: false
t.bigint "genre_id", null: false
end
create_table "groups_memberships", id: false, force: :cascade do |t|
t.bigint "user_id", null: false
t.bigint "group_id", null: false
end
create_table "playlists", id: :serial, force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "name"
t.string "link"
t.text "description"
t.bigint "group_id"
t.index ["group_id"], name: "index_playlists_on_group_id"
end
create_table "users", id: :serial, force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
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.string "name"
t.string "token"
t.date "birthday"
t.string "link"
t.string "playlistId"
t.string "country"
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
Give that a whirl (I haven't built this in an app, so there may be some errors in the code) and you should now be able to do your console run:
u = User.create([values])
u.genres (should return nil until you create some relationships)
etc.
I have two models: User and Listing.
I am trying to set up a one-to-many relationship between them via existing db columns.
class User < ApplicationRecord
has_many :listings
class Listing < ApplicationRecord
belongs_to :user, foreign_key: "user_id"
This is my migration:
class AddFkToListing < ActiveRecord::Migration[5.1]
def change
add_foreign_key :listings, :users, column: :user_id, primary_key: :user_id
end
end
But it created the foreign key in table users on column id.
Any idea how to do this properly?
Here is the DB schema:
create_table "listings", force: :cascade do |t|
t.integer "listing_id"
t.string "state"
t.integer "user_id"
t.string "title"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["listing_id"], name: "index_listings_on_listing_id", unique: true
end
create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
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"
t.string "request_token"
t.string "request_secret"
t.string "oauth_verifier"
t.string "oauth_token"
t.string "login_name"
t.integer "user_id"
t.index ["email"], name: "index_users_on_email", unique: true
end
Thank you so much!
Since you have a conventional foreign key field name (user_id in listings table), I believe this should work just fine for you:
class AddFkToListing < ActiveRecord::Migration[5.1]
def change
add_foreign_key :listings, :users
end
end
The syntax of add_foreign_key is:
first argument (:listings) - table which should contain foreign key
second argument (:users) - table which should be used for constraint
column: :user_id - specifies to which field of the listings table constraint should be applied
primary_key: - specifies the field of the users table to build a constraint
(see https://apidock.com/rails/ActiveRecord/ConnectionAdapters/SchemaStatements/add_foreign_key)
The primary_key: :user_id part in your example actually refers (tries to) to non-existing user_id field in users table.
create_table "addresses", force: :cascade do |t|
t.integer "user_id"
t.string "code_postal"
t.string "street_name"
t.string "street_number"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["code_postal"], name: "index_addresses_on_code_postal"
t.index ["user_id"], name: "index_addresses_on_user_id"
end
create_table "users", force: :cascade do |t|
t.string "name_first"
t.string "name_last"
t.date "date_birth"
t.string "address_email"
t.integer "address_primary_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
A user can have many addresses, but a user can only have one primary address.
How do I add a foreign key constraint on address_primary_id?
I'm assuming your associations look like this:
class User < ApplicationRecord
belongs_to :address_primary, class_name: Address
has_many :addresses
end
class Address < ApplicationRecord
belongs_to :user
has_one :user_as_primary, class_name: User, foreign_key: :address_primary_id
end
You can create a foreign key you want in a migration with this line:
add_foreign_key :users, :addresses, column: :address_primary_id
Here are the docs on foreign keys in migrations.
I am writing a Rails 4.2 app with models user, notecard, tag and tagging (for the m-2-m relationship).
A tag can have multiple notecards and a notecard can have multiple tags.
A card belongs to a user and a tag DOESN'T belong to a user.
How can I scope the tags that only a user has used?
I want to have an index of all tags and an index of the tags a user has actually used.
Thanks!
Here is the schema, as I don't have an idea on how to implement the where clause to index the tags a user has used.
to give you an idea, I'm looking for something like this
def index_of_used_tags
#Take all tags, return those that have cards from this user
end
class User < ActiveRecord::Base
has_many :comments
has_many :folders
has_many :cards
end
class Tag < ActiveRecord::Base
has_many :taggings
has_many :cards, through: :taggings
validates_presence_of :name
validates_uniqueness_of :name
end
class Folder < ActiveRecord::Base
belongs_to :user
validates_presence_of :name
validates_uniqueness_of :name, scope: :user_id
end
class Card < ActiveRecord::Base
belongs_to :user
belongs_to :folder
has_many :taggings
has_many :tags, through: :taggins
end
ActiveRecord::Schema.define(version: 20150604113358) do
enable_extension "plpgsql"
create_table "cards", force: :cascade do |t|
t.string "object"
t.text "content"
t.string "source"
t.integer "user_id"
t.integer "folder_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "cards", ["folder_id"], name: "index_cards_on_folder_id", using: :btree
add_index "cards", ["user_id"], name: "index_cards_on_user_id", using: :btree
create_table "comments", force: :cascade do |t|
t.text "content"
t.string "commentable_type"
t.integer "commentable_id"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "comments", ["user_id"], name: "index_comments_on_user_id", using: :btree
create_table "folders", force: :cascade do |t|
t.string "name"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "folders", ["user_id"], name: "index_folders_on_user_id", using: :btree
create_table "taggings", force: :cascade do |t|
t.integer "card_id"
t.integer "tag_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "taggings", ["card_id"], name: "index_taggings_on_card_id", using: :btree
add_index "taggings", ["tag_id"], name: "index_taggings_on_tag_id", using: :btree
create_table "tags", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "users", force: :cascade do |t|
t.string "fname"
t.string "lname"
t.boolean "admin", default: false
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
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
add_foreign_key "cards", "folders"
add_foreign_key "cards", "users"
add_foreign_key "comments", "users"
add_foreign_key "folders", "users"
add_foreign_key "taggings", "cards"
add_foreign_key "taggings", "tags" end
You can set up a has_many through relationship between User and Tag
class User < ActiveRecord::Base
has_many :comments
has_many :folders
has_many :cards
has_many :tags, through: :cards
end
Then user.tags would give you all the tags the user has used.
User.includes(:cards => :taggings).where('users.id = ?', current_user.id)
Try this query