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