Я продолжаю получать ActiveRecord::AssociationTypeMismatch, выполняя отношения «многие ко многим».
Я попытался изменить имена моделей, т.е.
has_many :subscribers, through: :rafflecontestants, source: :users
но это никуда меня не привело.
class Rafflecontestant < ApplicationRecord
belongs_to :user
belongs_to :raffle
end
class Raffle < ApplicationRecord
validates :name, presence: true
has_many :rafflecontestants
has_many :users, through: :rafflecontestants
end
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_many :rafflecontestants
has_many :raffles, through: :rafflecontestants
end
и моя схема:
create_table "rafflecontestants", force: :cascade do |t|
t.integer "user_id"
t.integer "raffle_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["raffle_id"], name: "index_rafflecontestants_on_raffle_id"
t.index ["user_id"], name: "index_rafflecontestants_on_user_id"
end
create_table "raffles", force: :cascade do |t|
t.string "name"
t.text "description"
t.integer "winner"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "created_by"
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", null: false
t.datetime "updated_at", null: false
t.boolean "is_admin"
t.string "authentication_token", limit: 30
t.index ["authentication_token"], name: "index_users_on_authentication_token", unique: true
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
Шаги для воспроизведения в консоли:
u = User.create(...)
r= Raffle.create(..., Rafflecontestants: [u])
ActiveRecord::AssociationTypeMismatch: Rafflecontestant(#66411640) expected, got #<User id: 1, email: "[email protected]", created_at: "2019-07-11 19:54:55", updated_at: "2019-07-11 19:54:55", is_admin: false, authentication_token: "yeU3mgQQPyXqDrMBFPAY"> which is an instance of User(#73796060)
Я ожидаю добавления пользователей в розыгрыши с помощью таблицы Rafflecontestants.





Наследуется ли ApplicationRecord от ActiveRecord::Base?
Я попробовал это с помощью ActiveRecord::Base, и это сработало для меня
user = User.create!
user.raffles.create!(name: "new raffle")
raffle = Raffle.last
user.rafflecontestants
# [#<Rafflecontestant:0x00007fda70313948 id: 1, user_id: 1, raffle_id: 1>]
Вы не совсем подходите к этому правильно.
Эти два должны работать, больше информации в гиды
user = User.create(...)
raffle = Raffle.create(...)
raffle.users << user
или ...
user = User.create(...)
raffle = Raffle.create(...)
user.raffles << raffle
Если бы у вас были дополнительные атрибуты в модели соединения Rafflecontestant, вам пришлось бы сделать это
user = User.create(...)
raffle = Raffle.create(...)
Rafflecontestant.create(user: user, raffle: raffle, some_extra_attribute: 1)