To make more simple I do have
class User
has_many :questions, trough: votes
has_many :questions #(as the author)
has_many :votes
end
Forgot to add foreign_key when created, now I don't know how to add it to specific (has_many through) association
schema.rb
enable_extension "plpgsql"
create_table "answers", force: :cascade do |t|
t.text "body"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "question_id"
t.integer "user_id"
t.boolean "best", default: false
end
add_index "answers", ["question_id"], name: "index_answers_on_question_id", using: :btree
add_index "answers", ["user_id"], name: "index_answers_on_user_id", using: :btree
create_table "attachments", force: :cascade do |t|
t.string "file"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "attachable_id"
t.string "attachable_type"
end
add_index "attachments", ["attachable_id", "attachable_type"], name: "index_attachments_on_attachable_id_and_attachable_type", using: :btree
create_table "questions", force: :cascade do |t|
t.string "title"
t.text "body"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
end
add_index "questions", ["title"], name: "index_questions_on_title", using: :btree
add_index "questions", ["user_id"], name: "index_questions_on_user_id", using: :btree
create_table "users", force: :cascade do |t|
t.string "name"
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.inet "current_sign_in_ip"
t.inet "last_sign_in_ip"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
add_index "users", ["name"], name: "index_users_on_name", unique: true, using: :btree
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
create_table "votes", force: :cascade do |t|
t.integer "votable_id"
t.string "votable_type"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "votes", ["user_id", "votable_id"], name: "index_votes_on_user_id_and_votable_id", unique: true, using: :btree
add_foreign_key "answers", "questions", on_delete: :cascade
add_foreign_key "questions", "users", on_delete: :cascade
end
Run this command on your console
rails g migration AddForeignKeyToVotes user:references question:references
This will generate a xxxx_add_foreign_key_to_votes.rb file under db/migrate/ with following contents
class AddForeignKeyToVotes < ActiveRecord::Migration
def change
add_reference :votes, :user, index: true, foreign_key: true
add_reference :votes, :question, index: true, foreign_key: true
end
end
Are you really need foreign keys?
Many of Rails developers are comfortable with the way Rails handles relationships in the application rather than the database.
for your case:
class User
has_many :questions, trough: votes
has_many :questions #(as the author)
has_many :votes
end
if votes table has question_id and user_id that is enough to define the relation without any foreign keys, unless you have a reason and really need this foreign keys to be defined database level.
Read THIS SECTION carefully, Rails is using Convention over Configuration.
As a small example: how your User model know which table to query in and retrieve the data, without any configuration it search for table with same name users (convention) and use it, same for foreign keys.
According to your comment you have a model something as Stackoverflow, you have a User who can ask a Question and can answer a Question in this case you may have something like:
class User
has_many :asked_questions, class_name: 'Question' # user can ask many questions
has_many :voted_questions, through: :votes, source: 'question'
has_many :votes # to get all votes this user did
end
class Question
has_many :votes # to get all votes for a question
belongs_to :user
end
class Vote
belongs_to :user
belongs_to :question
end
Database will be something like:
# Table User
# Table Question (user_id)
# Table Vote (user_id, question_id)
Let's say you want to get Questions user asked they it will be:
user = User.first
user.asked_questions
if you want get Questions who user votes for:
user.voted_questions
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 have a question about reaching across multiple association levels, if anyone could help me out?
I have a warehouse, and I'm trying to call up an inventory of products by their tag numbers. So far I can through and use methods defined in the Tag model, and grab associated Product name attributes easily enough:
#Warehouse.rb
has_many :tags, as: :location
has_many :products, through: :tags
#Tag.rb
belongs_to product #
#polymorphic
belongs_to :trackable, polymorphic: true
belongs_to :location, polymorphic: true
Now when I try to get the primary category of that inventory item (belongs_to :product), I keep running into issues; I want to define the category in my iterative inventory list, and have the option to sort/group by it:
#Product.rb
has_one :primary_category_assignment
has_many :rfid_tags, as: :trackable
Each product is assigned to a category - associations are warehouses > tags > products > category_assignment > category. I'm trying to drill down to the level of the last two.
#CategoryAssignment.rb #this is a join table
belongs_to :product, required: true
belongs_to :category, required: true
#Category.rb
has_many :products, through: :category_assignment
Here's some of the accompanying code I'm using to get this set up:
#warehouse_controller.rb
def index
#tags = #warehouse.tags.all.joins(:ancestor_product)
end
#warehouses/index.html.erb
<p><% #tags.each do |tag| %>
tag: <%= tag.number %>,
location_id <%= tag.location_id %>,
<%= tag.product.name %>,
Purchased at $<%= tag.product.purchase_price %></p> #grabs an attribute from the product level
#Next level would reach through the category assignment join table (category_id) to grab the category, or at least order by the category assignment
Schema below for reference:
#schema
create_table "categories", force: :cascade do |t|
t.string "name", null: false
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "position"
end
add_index "categories", ["position"], name: "index_categories_on_position", using: :btree
create_table "category_assignments", force: :cascade do |t|
t.integer "product_id"
t.integer "category_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "primary", default: false
end
create_table "products", force: :cascade do |t|
t.string "name", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.float "purchase_price"
t.text "description"
t.datetime "purchase_date"
end
add_index "category_assignments", ["category_id"], name: "index_category_assignments_on_category_id", using: :btree
add_index "category_assignments", ["product_id"], name: "index_category_assignments_on_product_id", using: :btree
create_table "tags", force: :cascade do |t|
t.string "number", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "location_type"
t.integer "location_id"
t.string "trackable_type"
t.integer "trackable_id"
t.integer "product_id"
end
add_index "tags", ["product_id"], name: "index_rfid_tags_on_ancestor_product_id", using: :btree
add_index "tags", ["location_id"], name: "index_tags_on_location_id", using: :btree
add_index "tags", ["location_type"], name: "index_tags_on_location_type", using: :btree
add_index "tags", ["trackable_id"], name: "index_tags_on_trackable_id", using: :btree
add_index "tags", ["trackable_type"], name: "index_tags_on_trackable_type", using: :btree
add_index "tags", ["user_id"], name: "index_tags_on_user_id", using: :btree
create_table "warehouses", force: :cascade do |t|
t.string "name", null: false
end
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
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.
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