Я пытаюсь добавить запись в таблицу соединений user_logs
, которая объединяет user_actions
и users
. Однако, когда я пытаюсь добавить элемент со следующим кодом, я получаю сообщение об ошибке ниже. Почему я получаю эту ошибку? Я включил модели и схему ниже для справки.Rails 4 ActiveRecord :: AssociationTypeMismatch
2.0.0-p353 :001 > user = User.first
2.0.0-p353 :002 > action = UserAction.first
2.0.0-p353 :003 > user.user_logs
UserLog Load (0.3ms) SELECT `user_logs`.* FROM `user_logs` WHERE `user_logs`.`user_id` = 1
=> #<ActiveRecord::Associations::CollectionProxy []>
2.0.0-p353 :004 > user.user_logs << action
(0.3ms) BEGIN
(0.2ms) ROLLBACK
ActiveRecord::AssociationTypeMismatch: UserLog(#2156995120) expected, got UserAction(#2177269140)
пользователя Модель
class User < ActiveRecord::Base
...
has_many :user_logs
has_many :user_actions, :through => :user_logs
end
Действия пользователя Модель
class UserAction < ActiveRecord::Base
has_many :user_logs
has_many :users, :through => :user_logs
end
пользователя Журналы Модель
class UserLog < ActiveRecord::Base
belongs_to :user
belongs_to :user_action
end
Схема
create_table "user_actions", force: true do |t|
t.string "action", limit: 40, default: "", null: false
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "user_actions", ["action"], name: "index_user_actions_on_action", unique: true, using: :btree
create_table "user_logs", force: true do |t|
t.integer "user_id"
t.integer "user_action_id"
t.datetime "created_at"
t.datetime "updated_at"
end
Я сделал уверен, что 'cache_classes' был установлен в' 'true' в конфигурации/среда/test.rb': http://stackoverflow.com/questions/5954690/strange-activerecordassociationtypemismatch , Это не устранило проблему. – evanvee