Build one-to-many association with ActiveRecord using alias for model name
I am building out a musician social site. Users can create bands, and they are considered from then on the manager of the band. I want to refer to a bands instance of a user as the manager.
Despite specifying the relationship in the models, and specifying class_name: "User" and foreign_key: "manager", the app will not save a new Band to the database.
user.rb
class User < ApplicationRecord
has_many :bands, foreign_key: 'manager'
validates_presence_of :name
validates_presence_of :password_digest
validates_presence_of :username
validates_uniqueness_of :username
has_secure_password
def admin?
self.role == "admin"
end
end
band.rb
class Band < ApplicationRecord
belongs_to :manager, class_name: 'User', foreign_key: 'manager'
validates_presence_of :name
validates_uniqueness_of :name
validates_presence_of :description
end
schema.rb
ActiveRecord::Schema.define(version: 2018_12_27_040935) do
create_table "bands", force: :cascade do |t|
t.string "name"
t.text "description"
t.integer "manager_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["manager_id"], name: "index_bands_on_manager_id"
end
create_table "users", force: :cascade do |t|
t.string "name"
t.string "username"
t.string "password_digest"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "role", default: "user"
end
end
seeds.rb
Edited seeds.rb
User.destroy_all
Band.destroy_all
user = User.new(name: 'John')
user.save
user = User.find_by_name('John')
user_band = user.bands.new(name: 'Band #1', description: 'My first band')
if user_band.save
puts "Done"
else
puts "Incomplete"
end
I run rails db:seed, and expect to see 'Done' in the console.
I get 'Incomplete'.
Testing in the rails console, the band instance has the error of "Manager must exist"
Also, a single user instance exists, with and id of 1.
ruby-on-rails ruby activerecord
add a comment |
I am building out a musician social site. Users can create bands, and they are considered from then on the manager of the band. I want to refer to a bands instance of a user as the manager.
Despite specifying the relationship in the models, and specifying class_name: "User" and foreign_key: "manager", the app will not save a new Band to the database.
user.rb
class User < ApplicationRecord
has_many :bands, foreign_key: 'manager'
validates_presence_of :name
validates_presence_of :password_digest
validates_presence_of :username
validates_uniqueness_of :username
has_secure_password
def admin?
self.role == "admin"
end
end
band.rb
class Band < ApplicationRecord
belongs_to :manager, class_name: 'User', foreign_key: 'manager'
validates_presence_of :name
validates_uniqueness_of :name
validates_presence_of :description
end
schema.rb
ActiveRecord::Schema.define(version: 2018_12_27_040935) do
create_table "bands", force: :cascade do |t|
t.string "name"
t.text "description"
t.integer "manager_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["manager_id"], name: "index_bands_on_manager_id"
end
create_table "users", force: :cascade do |t|
t.string "name"
t.string "username"
t.string "password_digest"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "role", default: "user"
end
end
seeds.rb
Edited seeds.rb
User.destroy_all
Band.destroy_all
user = User.new(name: 'John')
user.save
user = User.find_by_name('John')
user_band = user.bands.new(name: 'Band #1', description: 'My first band')
if user_band.save
puts "Done"
else
puts "Incomplete"
end
I run rails db:seed, and expect to see 'Done' in the console.
I get 'Incomplete'.
Testing in the rails console, the band instance has the error of "Manager must exist"
Also, a single user instance exists, with and id of 1.
ruby-on-rails ruby activerecord
add a comment |
I am building out a musician social site. Users can create bands, and they are considered from then on the manager of the band. I want to refer to a bands instance of a user as the manager.
Despite specifying the relationship in the models, and specifying class_name: "User" and foreign_key: "manager", the app will not save a new Band to the database.
user.rb
class User < ApplicationRecord
has_many :bands, foreign_key: 'manager'
validates_presence_of :name
validates_presence_of :password_digest
validates_presence_of :username
validates_uniqueness_of :username
has_secure_password
def admin?
self.role == "admin"
end
end
band.rb
class Band < ApplicationRecord
belongs_to :manager, class_name: 'User', foreign_key: 'manager'
validates_presence_of :name
validates_uniqueness_of :name
validates_presence_of :description
end
schema.rb
ActiveRecord::Schema.define(version: 2018_12_27_040935) do
create_table "bands", force: :cascade do |t|
t.string "name"
t.text "description"
t.integer "manager_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["manager_id"], name: "index_bands_on_manager_id"
end
create_table "users", force: :cascade do |t|
t.string "name"
t.string "username"
t.string "password_digest"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "role", default: "user"
end
end
seeds.rb
Edited seeds.rb
User.destroy_all
Band.destroy_all
user = User.new(name: 'John')
user.save
user = User.find_by_name('John')
user_band = user.bands.new(name: 'Band #1', description: 'My first band')
if user_band.save
puts "Done"
else
puts "Incomplete"
end
I run rails db:seed, and expect to see 'Done' in the console.
I get 'Incomplete'.
Testing in the rails console, the band instance has the error of "Manager must exist"
Also, a single user instance exists, with and id of 1.
ruby-on-rails ruby activerecord
I am building out a musician social site. Users can create bands, and they are considered from then on the manager of the band. I want to refer to a bands instance of a user as the manager.
Despite specifying the relationship in the models, and specifying class_name: "User" and foreign_key: "manager", the app will not save a new Band to the database.
user.rb
class User < ApplicationRecord
has_many :bands, foreign_key: 'manager'
validates_presence_of :name
validates_presence_of :password_digest
validates_presence_of :username
validates_uniqueness_of :username
has_secure_password
def admin?
self.role == "admin"
end
end
band.rb
class Band < ApplicationRecord
belongs_to :manager, class_name: 'User', foreign_key: 'manager'
validates_presence_of :name
validates_uniqueness_of :name
validates_presence_of :description
end
schema.rb
ActiveRecord::Schema.define(version: 2018_12_27_040935) do
create_table "bands", force: :cascade do |t|
t.string "name"
t.text "description"
t.integer "manager_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["manager_id"], name: "index_bands_on_manager_id"
end
create_table "users", force: :cascade do |t|
t.string "name"
t.string "username"
t.string "password_digest"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "role", default: "user"
end
end
seeds.rb
Edited seeds.rb
User.destroy_all
Band.destroy_all
user = User.new(name: 'John')
user.save
user = User.find_by_name('John')
user_band = user.bands.new(name: 'Band #1', description: 'My first band')
if user_band.save
puts "Done"
else
puts "Incomplete"
end
I run rails db:seed, and expect to see 'Done' in the console.
I get 'Incomplete'.
Testing in the rails console, the band instance has the error of "Manager must exist"
Also, a single user instance exists, with and id of 1.
ruby-on-rails ruby activerecord
ruby-on-rails ruby activerecord
edited Dec 28 '18 at 18:48
asked Dec 28 '18 at 2:42
John Mark
116
116
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Update associations as follows:
band.rb
belongs_to :manager, class_name: 'User', foreign_key: 'manager_id'
user.rb
has_many :bands, foreign_key: 'manager_id'
Before creating band, manager should be present in user table.
manager = User.find(1)
band = manager.bands.new(name: "Jams", description: "Hey, its our first band.")
if band.save
puts "Done"
else
puts "Incomplete"
end
Have tried, will not work. Receive errors.ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: main.managers: INSERT INTO "bands" ("name", "description", "manager_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)
– John Mark
Dec 28 '18 at 5:42
manager = User.find(1)...this will return manager and then create brand as manager.bands.new(name: 'My band')
– Ni3
Dec 28 '18 at 5:46
where are you saying thatuser = User.find(1)should go?
– John Mark
Dec 28 '18 at 5:48
Replace this with new queries: jms = Band.new(name: "Jams", description: "Hey, its our first band.", manager_id: 1)
– Ni3
Dec 28 '18 at 5:50
1
If your application is onboarding then reset database and run migrations again and seed also. Might be this will help.
– Ni3
Dec 28 '18 at 19:22
|
show 7 more comments
band.rb
belongs_to :manager, class_name: 'User', foreign_key: 'manager_id'
user.rb
has_many :bands, foreign_key: 'manager_id'
To test try this:
manager = User.create(...user_information)
jms = Band.new(name: "Jams", description: "Hey, its our first band.")
jms.manager = manager
jms.save
If it fails, what is the output of jms.errors?
Thanks Keith. Get an error as follows.ActiveRecord::StatementInvalid (SQLite3::SQLException: no such table: main.managers: INSERT INTO "bands" ("name", "description", "created_at", "updated_at") VALUES (?, ?, ?, ?))
– John Mark
Dec 28 '18 at 7:33
add a comment |
Okay, so I changed my migration file, which apparently was causing the issue. The schema.rb looks identical, but the seeds.rb is now this…
models
user.rb
has_many :bands, foreign_key: 'manager_id'
band.rb
belongs_to :manager, class_name: 'User', foreign_key: 'manager_id'
seeds.rb
User.destroy_all
Band.destroy_all
user = User.new(name: 'John', username: "johnny", password: "nice")
user.save
user = User.find_by_name('John')
user_band = Band.new(name: 'Band #1', description: 'My first band', manager_id: user.id)
if user_band.save
puts "Done"
else
puts "Incomplete"
end
The errors were: ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: main.managers: INSERT INTO "bands" ("name", "description", "manager_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)
Changed migration file for bands
Previously
create_table :bands do |t|
t.belongs_to :manager, foreign_key: true
t.string :name
t.text :description
t.timestamps
end
Changed To
create_table :bands do |t|
t.belongs_to :manager
t.string :name
t.text :description
t.timestamps
end
Removed option , foreign_key: true, Dropped DB, ran migrations, and now.
It Works…
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53953113%2fbuild-one-to-many-association-with-activerecord-using-alias-for-model-name%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Update associations as follows:
band.rb
belongs_to :manager, class_name: 'User', foreign_key: 'manager_id'
user.rb
has_many :bands, foreign_key: 'manager_id'
Before creating band, manager should be present in user table.
manager = User.find(1)
band = manager.bands.new(name: "Jams", description: "Hey, its our first band.")
if band.save
puts "Done"
else
puts "Incomplete"
end
Have tried, will not work. Receive errors.ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: main.managers: INSERT INTO "bands" ("name", "description", "manager_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)
– John Mark
Dec 28 '18 at 5:42
manager = User.find(1)...this will return manager and then create brand as manager.bands.new(name: 'My band')
– Ni3
Dec 28 '18 at 5:46
where are you saying thatuser = User.find(1)should go?
– John Mark
Dec 28 '18 at 5:48
Replace this with new queries: jms = Band.new(name: "Jams", description: "Hey, its our first band.", manager_id: 1)
– Ni3
Dec 28 '18 at 5:50
1
If your application is onboarding then reset database and run migrations again and seed also. Might be this will help.
– Ni3
Dec 28 '18 at 19:22
|
show 7 more comments
Update associations as follows:
band.rb
belongs_to :manager, class_name: 'User', foreign_key: 'manager_id'
user.rb
has_many :bands, foreign_key: 'manager_id'
Before creating band, manager should be present in user table.
manager = User.find(1)
band = manager.bands.new(name: "Jams", description: "Hey, its our first band.")
if band.save
puts "Done"
else
puts "Incomplete"
end
Have tried, will not work. Receive errors.ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: main.managers: INSERT INTO "bands" ("name", "description", "manager_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)
– John Mark
Dec 28 '18 at 5:42
manager = User.find(1)...this will return manager and then create brand as manager.bands.new(name: 'My band')
– Ni3
Dec 28 '18 at 5:46
where are you saying thatuser = User.find(1)should go?
– John Mark
Dec 28 '18 at 5:48
Replace this with new queries: jms = Band.new(name: "Jams", description: "Hey, its our first band.", manager_id: 1)
– Ni3
Dec 28 '18 at 5:50
1
If your application is onboarding then reset database and run migrations again and seed also. Might be this will help.
– Ni3
Dec 28 '18 at 19:22
|
show 7 more comments
Update associations as follows:
band.rb
belongs_to :manager, class_name: 'User', foreign_key: 'manager_id'
user.rb
has_many :bands, foreign_key: 'manager_id'
Before creating band, manager should be present in user table.
manager = User.find(1)
band = manager.bands.new(name: "Jams", description: "Hey, its our first band.")
if band.save
puts "Done"
else
puts "Incomplete"
end
Update associations as follows:
band.rb
belongs_to :manager, class_name: 'User', foreign_key: 'manager_id'
user.rb
has_many :bands, foreign_key: 'manager_id'
Before creating band, manager should be present in user table.
manager = User.find(1)
band = manager.bands.new(name: "Jams", description: "Hey, its our first band.")
if band.save
puts "Done"
else
puts "Incomplete"
end
edited Dec 28 '18 at 8:37
gaotongfei
6815
6815
answered Dec 28 '18 at 5:39
Ni3
1768
1768
Have tried, will not work. Receive errors.ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: main.managers: INSERT INTO "bands" ("name", "description", "manager_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)
– John Mark
Dec 28 '18 at 5:42
manager = User.find(1)...this will return manager and then create brand as manager.bands.new(name: 'My band')
– Ni3
Dec 28 '18 at 5:46
where are you saying thatuser = User.find(1)should go?
– John Mark
Dec 28 '18 at 5:48
Replace this with new queries: jms = Band.new(name: "Jams", description: "Hey, its our first band.", manager_id: 1)
– Ni3
Dec 28 '18 at 5:50
1
If your application is onboarding then reset database and run migrations again and seed also. Might be this will help.
– Ni3
Dec 28 '18 at 19:22
|
show 7 more comments
Have tried, will not work. Receive errors.ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: main.managers: INSERT INTO "bands" ("name", "description", "manager_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)
– John Mark
Dec 28 '18 at 5:42
manager = User.find(1)...this will return manager and then create brand as manager.bands.new(name: 'My band')
– Ni3
Dec 28 '18 at 5:46
where are you saying thatuser = User.find(1)should go?
– John Mark
Dec 28 '18 at 5:48
Replace this with new queries: jms = Band.new(name: "Jams", description: "Hey, its our first band.", manager_id: 1)
– Ni3
Dec 28 '18 at 5:50
1
If your application is onboarding then reset database and run migrations again and seed also. Might be this will help.
– Ni3
Dec 28 '18 at 19:22
Have tried, will not work. Receive errors.
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: main.managers: INSERT INTO "bands" ("name", "description", "manager_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)– John Mark
Dec 28 '18 at 5:42
Have tried, will not work. Receive errors.
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: main.managers: INSERT INTO "bands" ("name", "description", "manager_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)– John Mark
Dec 28 '18 at 5:42
manager = User.find(1)...this will return manager and then create brand as manager.bands.new(name: 'My band')
– Ni3
Dec 28 '18 at 5:46
manager = User.find(1)...this will return manager and then create brand as manager.bands.new(name: 'My band')
– Ni3
Dec 28 '18 at 5:46
where are you saying that
user = User.find(1) should go?– John Mark
Dec 28 '18 at 5:48
where are you saying that
user = User.find(1) should go?– John Mark
Dec 28 '18 at 5:48
Replace this with new queries: jms = Band.new(name: "Jams", description: "Hey, its our first band.", manager_id: 1)
– Ni3
Dec 28 '18 at 5:50
Replace this with new queries: jms = Band.new(name: "Jams", description: "Hey, its our first band.", manager_id: 1)
– Ni3
Dec 28 '18 at 5:50
1
1
If your application is onboarding then reset database and run migrations again and seed also. Might be this will help.
– Ni3
Dec 28 '18 at 19:22
If your application is onboarding then reset database and run migrations again and seed also. Might be this will help.
– Ni3
Dec 28 '18 at 19:22
|
show 7 more comments
band.rb
belongs_to :manager, class_name: 'User', foreign_key: 'manager_id'
user.rb
has_many :bands, foreign_key: 'manager_id'
To test try this:
manager = User.create(...user_information)
jms = Band.new(name: "Jams", description: "Hey, its our first band.")
jms.manager = manager
jms.save
If it fails, what is the output of jms.errors?
Thanks Keith. Get an error as follows.ActiveRecord::StatementInvalid (SQLite3::SQLException: no such table: main.managers: INSERT INTO "bands" ("name", "description", "created_at", "updated_at") VALUES (?, ?, ?, ?))
– John Mark
Dec 28 '18 at 7:33
add a comment |
band.rb
belongs_to :manager, class_name: 'User', foreign_key: 'manager_id'
user.rb
has_many :bands, foreign_key: 'manager_id'
To test try this:
manager = User.create(...user_information)
jms = Band.new(name: "Jams", description: "Hey, its our first band.")
jms.manager = manager
jms.save
If it fails, what is the output of jms.errors?
Thanks Keith. Get an error as follows.ActiveRecord::StatementInvalid (SQLite3::SQLException: no such table: main.managers: INSERT INTO "bands" ("name", "description", "created_at", "updated_at") VALUES (?, ?, ?, ?))
– John Mark
Dec 28 '18 at 7:33
add a comment |
band.rb
belongs_to :manager, class_name: 'User', foreign_key: 'manager_id'
user.rb
has_many :bands, foreign_key: 'manager_id'
To test try this:
manager = User.create(...user_information)
jms = Band.new(name: "Jams", description: "Hey, its our first band.")
jms.manager = manager
jms.save
If it fails, what is the output of jms.errors?
band.rb
belongs_to :manager, class_name: 'User', foreign_key: 'manager_id'
user.rb
has_many :bands, foreign_key: 'manager_id'
To test try this:
manager = User.create(...user_information)
jms = Band.new(name: "Jams", description: "Hey, its our first band.")
jms.manager = manager
jms.save
If it fails, what is the output of jms.errors?
answered Dec 28 '18 at 7:21
Keith Mattix
359312
359312
Thanks Keith. Get an error as follows.ActiveRecord::StatementInvalid (SQLite3::SQLException: no such table: main.managers: INSERT INTO "bands" ("name", "description", "created_at", "updated_at") VALUES (?, ?, ?, ?))
– John Mark
Dec 28 '18 at 7:33
add a comment |
Thanks Keith. Get an error as follows.ActiveRecord::StatementInvalid (SQLite3::SQLException: no such table: main.managers: INSERT INTO "bands" ("name", "description", "created_at", "updated_at") VALUES (?, ?, ?, ?))
– John Mark
Dec 28 '18 at 7:33
Thanks Keith. Get an error as follows.
ActiveRecord::StatementInvalid (SQLite3::SQLException: no such table: main.managers: INSERT INTO "bands" ("name", "description", "created_at", "updated_at") VALUES (?, ?, ?, ?))– John Mark
Dec 28 '18 at 7:33
Thanks Keith. Get an error as follows.
ActiveRecord::StatementInvalid (SQLite3::SQLException: no such table: main.managers: INSERT INTO "bands" ("name", "description", "created_at", "updated_at") VALUES (?, ?, ?, ?))– John Mark
Dec 28 '18 at 7:33
add a comment |
Okay, so I changed my migration file, which apparently was causing the issue. The schema.rb looks identical, but the seeds.rb is now this…
models
user.rb
has_many :bands, foreign_key: 'manager_id'
band.rb
belongs_to :manager, class_name: 'User', foreign_key: 'manager_id'
seeds.rb
User.destroy_all
Band.destroy_all
user = User.new(name: 'John', username: "johnny", password: "nice")
user.save
user = User.find_by_name('John')
user_band = Band.new(name: 'Band #1', description: 'My first band', manager_id: user.id)
if user_band.save
puts "Done"
else
puts "Incomplete"
end
The errors were: ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: main.managers: INSERT INTO "bands" ("name", "description", "manager_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)
Changed migration file for bands
Previously
create_table :bands do |t|
t.belongs_to :manager, foreign_key: true
t.string :name
t.text :description
t.timestamps
end
Changed To
create_table :bands do |t|
t.belongs_to :manager
t.string :name
t.text :description
t.timestamps
end
Removed option , foreign_key: true, Dropped DB, ran migrations, and now.
It Works…
add a comment |
Okay, so I changed my migration file, which apparently was causing the issue. The schema.rb looks identical, but the seeds.rb is now this…
models
user.rb
has_many :bands, foreign_key: 'manager_id'
band.rb
belongs_to :manager, class_name: 'User', foreign_key: 'manager_id'
seeds.rb
User.destroy_all
Band.destroy_all
user = User.new(name: 'John', username: "johnny", password: "nice")
user.save
user = User.find_by_name('John')
user_band = Band.new(name: 'Band #1', description: 'My first band', manager_id: user.id)
if user_band.save
puts "Done"
else
puts "Incomplete"
end
The errors were: ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: main.managers: INSERT INTO "bands" ("name", "description", "manager_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)
Changed migration file for bands
Previously
create_table :bands do |t|
t.belongs_to :manager, foreign_key: true
t.string :name
t.text :description
t.timestamps
end
Changed To
create_table :bands do |t|
t.belongs_to :manager
t.string :name
t.text :description
t.timestamps
end
Removed option , foreign_key: true, Dropped DB, ran migrations, and now.
It Works…
add a comment |
Okay, so I changed my migration file, which apparently was causing the issue. The schema.rb looks identical, but the seeds.rb is now this…
models
user.rb
has_many :bands, foreign_key: 'manager_id'
band.rb
belongs_to :manager, class_name: 'User', foreign_key: 'manager_id'
seeds.rb
User.destroy_all
Band.destroy_all
user = User.new(name: 'John', username: "johnny", password: "nice")
user.save
user = User.find_by_name('John')
user_band = Band.new(name: 'Band #1', description: 'My first band', manager_id: user.id)
if user_band.save
puts "Done"
else
puts "Incomplete"
end
The errors were: ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: main.managers: INSERT INTO "bands" ("name", "description", "manager_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)
Changed migration file for bands
Previously
create_table :bands do |t|
t.belongs_to :manager, foreign_key: true
t.string :name
t.text :description
t.timestamps
end
Changed To
create_table :bands do |t|
t.belongs_to :manager
t.string :name
t.text :description
t.timestamps
end
Removed option , foreign_key: true, Dropped DB, ran migrations, and now.
It Works…
Okay, so I changed my migration file, which apparently was causing the issue. The schema.rb looks identical, but the seeds.rb is now this…
models
user.rb
has_many :bands, foreign_key: 'manager_id'
band.rb
belongs_to :manager, class_name: 'User', foreign_key: 'manager_id'
seeds.rb
User.destroy_all
Band.destroy_all
user = User.new(name: 'John', username: "johnny", password: "nice")
user.save
user = User.find_by_name('John')
user_band = Band.new(name: 'Band #1', description: 'My first band', manager_id: user.id)
if user_band.save
puts "Done"
else
puts "Incomplete"
end
The errors were: ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: main.managers: INSERT INTO "bands" ("name", "description", "manager_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)
Changed migration file for bands
Previously
create_table :bands do |t|
t.belongs_to :manager, foreign_key: true
t.string :name
t.text :description
t.timestamps
end
Changed To
create_table :bands do |t|
t.belongs_to :manager
t.string :name
t.text :description
t.timestamps
end
Removed option , foreign_key: true, Dropped DB, ran migrations, and now.
It Works…
answered Dec 28 '18 at 19:22
John Mark
116
116
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53953113%2fbuild-one-to-many-association-with-activerecord-using-alias-for-model-name%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown