This question was discussed numerous times, however I run into an issue that I could not find an answer to.
I am building a login system where one Member(:class) "bails for" a new Member. Internally, they are referenced as "member" and "candidate" respectively. Until the member has accepted the bail, a BailRequest(:class) is listed in the according table.
According to the rails guide, the right way to tell rails about the class refering to would be
class BailRequest < ApplicationRecord
belongs_to :candidate, class_name: "Member"
belongs_to :member
end
where class_name: "Member" should tell rails that BailRequest.candidate is of class: Member. The very same approach worked in the member class flawlessly
class Member < ApplicationRecord
belongs_to :bail, class_name: "Member", optional: true
has_many :associates, class_name: "Member", foreign_key: "bail_id"
end
However, this time when I want to save a BailRequest to the database, I get an ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: main.candidates.
It looks like ActiveRecord is expecting a table called "candidates" here. What am I missing?
$ rails -v
Rails 5.2.1
[$ ruby -v
ruby 2.3.1p112 (2016-04-26) [i386-linux-gnu]]
The schema.rb shows the following after migration
create_table "bail_requests", force: :cascade do |t|
t.integer "candidate_id"
t.integer "member_id"
t.string "message", limit: 100
t.boolean "accepted"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["candidate_id"], name: "index_bail_requests_on_candidate_id", unique: true
t.index ["member_id"], name: "index_bail_requests_on_member_id"
end
create_table "members", force: :cascade do |t|
t.string "email", limit: 50, null: false
t.string "password_digest", null: false
t.integer "bail_id"
t.string "username", limit: 50
t.string "first_name", limit: 50
t.string "last_name", limit: 50
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["email"], name: "index_members_on_email", unique: true
t.index ["username"], name: "index_members_on_username", unique: true
end
Logic:
When a Member m registers, m has to reference a Member n by name.
m.bail equals nil/null and a BailRequest br is created with
br.member = n and br.candidate = m
If member accepts the bail, br.accepted is set to true. m.bail is set to n and m is in n.associates
In the migration file, I changed
t.references :candidate, index: {:unique=>true}
to
t.integer :candidate_id, index: {:unique=>true}, foreign_key: true
and rerun the migration. It is now working on the rails console. Thanks to arieljuod for your effort.
Related
So I am currently assigned a task in Ruby which I have never used before and I've run into a very strange problem. I have this migration for a model message, which has an index on chat_id and number.
class CreateMessages < ActiveRecord::Migration[5.2]
def change
create_table :messages do |t|
t.references :chat, foreign_key: true, null: false
t.integer :number, null: false
t.string :body
#t.index [:chat_id, :number] doesn't work
t.timestamps
end
#add_index :messages, [:chat_id, :number] #doesn't work either
end
end
The end schema of both those migrations when running rails db:migrate is this
create_table "messages", force: :cascade do |t|
t.integer "chat_id", null: false
t.integer "number", null: false
t.string "body"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["chat_id", "number"], name: "index_messages_on_chat_id_and_number"
t.index ["chat_id"], name: "index_messages_on_chat_id"
end
Obviously, the second created index is completely useless. How do I prevent this from occuring and create only one index when migrating?
Ruby 2.7.6
Rails 5.2.8.1
This line is adding the index you wish to remove. It accepts an index parameter which defaults to true, change it to:
t.references :chat, foreign_key: true, null: false, index: false
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 column/foreign key, resolver_id, that I want to be able to have null values (ie: Rails Migration to make a column null => true). Let's say I have the following line in my migration:
def
change_column_null :bugs, :resolver_id, true
end
However, after running a successful migration (ie, generate the migration and run rails db:migrate), the schema remains unchanged, besides the version number:
t.integer "resolver_id"
whereas I am expecting:
t.integer "resolver_id" , null: true
Is there something I'm missing?
I've also tried using just change_column like so:
change_column :bugs, :resolver_id, :integer, null: true
However, this is still not reflected in the schema. The rails g migration and db:migrate work just fine, and the version number in the schema matches the latest migration.
For reference, here is my schema:
ActiveRecord::Schema.define(version: 20170502203934) do
create_table "bugs", force: :cascade do |t|
t.string "name"
t.text "error_msg"
t.text "description"
t.text "causes"
t.boolean "resolved"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
t.integer "resolver_id"
t.index ["resolver_id"], name: "index_bugs_on_resolver_id"
t.index ["user_id"], name: "index_bugs_on_user_id"
end
create_table "users", force: :cascade do |t|
t.string "username"
t.string "first_name"
t.string "last_name"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["username"], name: "index_users_on_username", unique: true
end
end
If relevant, the resolver_id foreign key is a reference a User model, ie:
class Bug < ApplicationRecord
# Associations
belongs_to :user
belongs_to :resolver, class_name: 'User'
end
null: true is the default behavior. You will never see it in your schema, you will see either null: false or nothing.
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
Hello I do have this two models and I would like to check that my model associations are working the way it should trough rails console.
I am not able to do the association work. The relationship is the following:
One Event has one rule and one rule belongs to one event. It could not be a rule without an event and it could not be a event without a rule.
Any idea how to test this with rails console?
MODEL 1:
class Event < ActiveRecord::Base
has_and_belongs_to_many :users
has_one :rule
has_many :grand_prixes
belongs_to :eventable, polymorphic: :true
end
MODEL 2
class Rule < ActiveRecord::Base
belongs_to :events
end
Rules' Schema:
create_table "rules", force: :cascade do |t|
t.boolean "abs"
t.boolean "tc"
t.boolean "allow_auto_clutch"
t.boolean "allow_sc"
t.boolean "allow_throttle_blip"
t.boolean "dynamic_track"
t.integer "damage_mult"
t.integer "fuel_rate"
t.integer "tyre_wear_rate"
t.integer "quali_percentage"
t.integer "min_valid_laps"
t.integer "event_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "rules", ["event_id"], name: "index_rules_on_event_id"
Events' Schema:
create_table "events", force: :cascade do |t|
t.string "event_type"
t.string "name", null: false
t.datetime "starting_date"
t.datetime "ending_date"
t.integer "eventable_id"
t.string "eventable_type"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "events", ["eventable_type", "eventable_id"], name: "index_events_on_eventable_type_and_eventable_id"
Thanks in advance.
I think your belongs_to :events should be singular to follow the rails convention :
class Rule < ActiveRecord::Base
belongs_to :event
end
The conventional name of a relation is always singular for belongs_to and has_one, and always plural for has_many.
Related documentation : http://guides.rubyonrails.org/association_basics.html#belongs-to-association-reference
EDIT : There much left to say
You wrote :
ev = Event.create(:name "test1").save
rule = Rule.create.save
create is already a new followed by a save. No need to save afterwards.
the syntax key: value is something very common in ruby, and should be well understood : you're actually writing a hash, equivalent to {:key => value}, but the syntax allows you to write key: value ONLY IF your key is a Symbol.
the columns eventable_type and eventable_id should be in the table rules, who is hosting the polymorphic relation with eventable things. Event should not have these columns, and event_id should not exist at all in rules.
Here's an example of what you can write in your console to create an Event and a Rule :
ev = Event.create(name: "test1")
rule = Rule.create(abs: true, event: ev)
Change your code:
class Rule < ActiveRecord::Base
belongs_to :event
end
With belongs_to you should use singular like event not events.
In console you can check association like:
Event.first.rule if Event.first.present?
For more details you should go through http://guides.rubyonrails.org/association_basics.html documentation.
Current code:
class Rule < ActiveRecord::Base
belongs_to :event
end
class Event < ActiveRecord::Base
has_and_belongs_to_many :users
has_one :rule
has_many :grand_prixes
belongs_to :eventable, polymorphic: :true
end
SCHEMA:
create_table "rules", force: :cascade do |t|
t.boolean "abs"
t.boolean "tc"
t.boolean "allow_auto_clutch"
t.boolean "allow_sc"
t.boolean "allow_throttle_blip"
t.boolean "dynamic_track"
t.integer "damage_mult"
t.integer "fuel_rate"
t.integer "tyre_wear_rate"
t.integer "quali_percentage"
t.integer "min_valid_laps"
t.integer "event_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "rules", ["event_id"], name: "index_rules_on_event_id", unique: true
create_table "events", force: :cascade do |t|
t.string "event_type"
t.string "name", null: false
t.datetime "starting_date"
t.datetime "ending_date"
t.integer "eventable_id"
t.string "eventable_type"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "events", ["eventable_type", "eventable_id"], name: "index_events_on_eventable_type_and_eventable_id"
Tested on console:
ev = Event.create(:name "test1").save
rule = Rule.create.save
No idea how to link it both through console.