【发布时间】:2014-02-24 14:07:07
【问题描述】:
我已经为这个问题苦苦挣扎了一天半了。我正在使用 Mailboxer gem 来创建一个非常简单的消息系统;与示例相同的一个 - https://github.com/RKushnir/mailboxer-app - 只是从这个 gem 开始。
我的计算机上确实有两个存储库并排放置。特定于邮箱的模型、迁移、初始化程序,我能找到的一切。这包括带有name 操作的用户模型。我什至尝试过用attr_accessible 来处理Notification 模型。
数据库是相同的。
当我在示例应用程序中保存对话后尝试在控制台/种子中创建消息时,这非常容易。保存对话后,我运行:
n = Message.new
n.sender = User.find(1)
n.subject = 'Hi'
n.body = 'Hello there.'
n.conversation_id = Conversation.find(1).id
n.save
然后繁荣。它工作得很好。另一方面,我的应用程序将发挥相同的作用;直到到达sender 字段。
当我尝试设置发件人时:
n.sender = User.find(1)
我收到此错误:NoMethodError: undefined method 'sender=' for #<Message:0x00000101708eb8>
为什么??
这快把我逼疯了。如果有人可以提供一些帮助,我将不胜感激。提前谢谢你。
消息/通知架构:
create_table "notifications", force: true do |t|
t.string "type"
t.text "body"
t.string "subject", default: ""
t.integer "sender_id", null: false
t.integer "conversation_id"
t.boolean "draft", default: false
t.datetime "updated_at", null: false
t.datetime "created_at", null: false
t.integer "notified_object_id"
t.string "notified_object_type"
t.string "notification_code"
t.string "attachment"
t.boolean "global", default: false
t.datetime "expires"
end
add_index "notifications", ["conversation_id"], name: "index_notifications_on_conversation_id", using: :btree
这是 gem 生成的内容,它也与示例应用程序相同。
【问题讨论】:
-
你能把你的消息模式代码。它说您的消息没有发件人字段。
-
困扰我的是——当然,没有发件人字段——但因为迁移是
t.references :sender,它创建了sender_id和sender_type字段,我可以通过这些字段进行设置示例应用程序的控制台中的发件人字段。但不是我的应用 -
而且,最重要的是,我尝试删除发件人字段并将其替换为简单的发件人 ID,然后进行迁移,但我仍然遇到相同的错误
-
所以sender是另一个表,所以有一个叫sender_id的foreign_key,你的代码有没有sender model
-
没有,但示例应用程序也没有。我想问题出在
t.references的函数中:sender。t.references有什么我不知道的地方吗?