我正在尝试为此创建简单的内部消息传递服务,我已使用设计进行身份验证,因此我有用户表。我为消息创建了一个消息表和一个名为 messages_users 的连接表。我想通过关联使用has_many,所以我的关联是这样的:-
用户.rb :-
has_many :sent_messages, class_name: "Message", foreign_key: "user_id"
has_many :message_users
has_many :received_messages, through: :message_users, source: :message
消息.rb:-
belongs_to :user
has_many :message_users
has_many :receivers, through: :message_users, source: :user
message_user.rb :-
class MessageUser < ActiveRecord::Base
belongs_to :user
belongs_to :message
end
和 schema.rb :-
ActiveRecord::Schema.define(version: 20130912100741) do
create_table "messages", force: true do |t|
t.text "subject"
t.text "body", limit: 2147483647
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "messages_users", id: false, force: true do |t|
t.integer "user_id", null: false
t.integer "message_id", null: false
end
add_index "messages_users", ["message_id"], name: "index_messages_users_on_message_id", using: :btree
add_index "messages_users", ["user_id"], name: "index_messages_users_on_user_id", using: :btree
create_table "users", force: true 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.integer "sign_in_count", default: 0
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "unconfirmed_email"
t.integer "failed_attempts", default: 0
t.string "unlock_token"
t.datetime "locked_at"
t.string "authentication_token"
t.datetime "created_at"
t.datetime "updated_at"
t.string "provider"
t.string "uid"
t.string "name"
end
add_index "users", ["authentication_token"], name: "index_users_on_authentication_token", unique: true, using: :btree
add_index "users", ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true, using: :btree
add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
add_index "users", ["unlock_token"], name: "index_users_on_unlock_token", unique: true, using: :btree
end
当我使用 user_object.sent_messages 时它工作正常,但是当我使用 user_object.received_messages 时它给了我Mysql2::Error: Table 'to_do.message_users' doesn't exist: SHOW FULL FIELDS FROM message_users ActiveRecord::StatementInvalid: Mysql2::Error:表 'to_do.message_users' 不存在错误。我只想通过而不是 habtm 使用 has_many。这里有几个关于stackoverflow的问题,但没有一个能解决我的问题,所以有什么建议吗?