使用模型名称的别名与ActiveRecord建立一对多关联

时间:2018-12-28 02:42:09

标签: ruby-on-rails ruby activerecord

我正在建立一个音乐家社交网站。用户可以创建乐队,然后从乐队的button开始对其进行考虑。我想将用户的band实例称为manager

尽管在模型中指定了关系,并指定了managerclass_name: "User",该应用仍不会将新的Band保存到数据库中。

user.rb

foreign_key: "manager"

band.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

schema.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

seeds.rb

编辑的seed.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

我运行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 ,希望在控制台中看到rails db:seed。 我得到'Done'

在rails控制台中测试,band实例的错误为'Incomplete'

此外,还存在一个用户实例,其ID为1。

3 个答案:

答案 0 :(得分:1)

更新关联如下:

band.rb

belongs_to :manager, class_name: 'User', foreign_key: 'manager_id'

user.rb

has_many :bands, foreign_key: 'manager_id'

在创建乐队之前,管理者应该出现在用户表中。

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

答案 1 :(得分:0)

band.rb

belongs_to :manager, class_name: 'User', foreign_key: 'manager_id'

user.rb

has_many :bands, foreign_key: 'manager_id'

要进行测试,请尝试以下操作:

manager = User.create(...user_information)
jms = Band.new(name: "Jams", description: "Hey, its our first band.")
jms.manager = manager
jms.save

如果失败,jms.errors的输出是什么?

答案 2 :(得分:0)

好的,所以我更改了迁移文件,这显然是导致问题的原因。 schema.rb看起来一样,但是seeds.rb现在是这个……

模型

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

错误为:ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: main.managers: INSERT INTO "bands" ("name", "description", "manager_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)

已更改乐队的迁移文件

以前

create_table :bands do |t|
    t.belongs_to :manager, foreign_key: true
    t.string :name
    t.text :description
    t.timestamps
end

更改为

create_table :bands do |t|
    t.belongs_to :manager
    t.string :name
    t.text :description
    t.timestamps
end

已删除选项, foreign_key: true,删除数据库,立即进行迁移。

有效...