Ruby on rails 向mailboxer迁移添加列
我想在迁移中添加两列,为mailboxer gem生成通知。当我将两者都放入创建通知的迁移中,然后运行迁移时,它将通过。然后我将它们输入到表单中,当我提交它们时,没有错误,并且日志显示它接受了它们。由于某种原因,当我试图显示它们时,它们没有显示出来,唯一显示的是作为迁移的一部分的原始事物,即主体和身体。我的问题是如何向迁移中添加列 下面是我的两列lat和long添加到通知部分的迁移Ruby on rails 向mailboxer迁移添加列,ruby-on-rails,ruby,gem,migration,message,Ruby On Rails,Ruby,Gem,Migration,Message,我想在迁移中添加两列,为mailboxer gem生成通知。当我将两者都放入创建通知的迁移中,然后运行迁移时,它将通过。然后我将它们输入到表单中,当我提交它们时,没有错误,并且日志显示它接受了它们。由于某种原因,当我试图显示它们时,它们没有显示出来,唯一显示的是作为迁移的一部分的原始事物,即主体和身体。我的问题是如何向迁移中添加列 下面是我的两列lat和long添加到通知部分的迁移 # This migration comes from mailboxer_engine (originally
# This migration comes from mailboxer_engine (originally 20110511145103)
class CreateMailboxer < ActiveRecord::Migration
def self.up
#Tables
#Conversations
create_table :conversations do |t|
t.column :subject, :string, :default => ""
t.column :created_at, :datetime, :null => false
t.column :updated_at, :datetime, :null => false
end
#Receipts
create_table :receipts do |t|
t.references :receiver, :polymorphic => true
t.column :notification_id, :integer, :null => false
t.column :read, :boolean, :default => false
t.column :trashed, :boolean, :default => false
t.column :deleted, :boolean, :default => false
t.column :mailbox_type, :string, :limit => 25
t.column :created_at, :datetime, :null => false
t.column :updated_at, :datetime, :null => false
end
#Notifications and Messages
create_table :notifications do |t|
t.column :type, :string
t.column :body, :text
t.column :subject, :string, :default => ""
t.column :lat, :text
t.column :long, :text
t.references :sender, :polymorphic => true
t.references :object, :polymorphic => true
t.column :conversation_id, :integer
t.column :draft, :boolean, :default => false
t.column :updated_at, :datetime, :null => false
t.column :created_at, :datetime, :null => false
end
#Indexes
#Conversations
#Receipts
add_index "receipts","notification_id"
#Messages
add_index "notifications","conversation_id"
#Foreign keys
#Conversations
#Receipts
add_foreign_key "receipts", "notifications", :name => "receipts_on_notification_id"
#Messages
add_foreign_key "notifications", "conversations", :name => "notifications_on_conversation_id"
end
def self.down
#Tables
remove_foreign_key "receipts", :name => "receipts_on_notification_id"
remove_foreign_key "notifications", :name => "notifications_on_conversation_id"
#Indexes
drop_table :receipts
drop_table :conversations
drop_table :notifications
end
end
这就是控制台中出现的内容,对于lat和long,您可以看到它显示null
Notification Load (0.1ms) SELECT "notifications".* FROM "notifications" ORDER BY "notifications"."id" DESC LIMIT 1
--- !ruby/object:Message
attributes:
id: 4
type: Message
body: game
subject: wiz
lat: !!null
long: !!null
sender_id: 1
sender_type: User
conversation_id: 2
draft: false
updated_at: 2013-03-10 04:37:54.984277000Z
created_at: 2013-03-10 04:37:54.984277000Z
notified_object_id: !!null
notified_object_type: !!null
notification_code: !!null
attachment: !!null
=> nil
我不确定我是否完全理解您在做什么,但听起来您可能没有将这两个新列添加到attr_accessible,因此无法保存它们。这是我要检查的第一件事(在模型中)
否则,请转到控制台,查看新列是否存在,以及其中是否包含数据。这将帮助您找到问题所在。我将列添加到attr_accessibe中,但没有做任何事情。当我在控制台中查看时,列会说!!我不知道你所说的专栏是什么意思。如果您只是在rails控制台中键入“Notification”。你看到了什么?还有,当您执行“通知。首先”时,您会看到什么。也许可以将其作为编辑添加到您的问题中。编辑我的帖子以包含控制台输出您可能需要查看日志以了解发生了什么。查看表单中传递给“create”操作的参数,确保这些lat/long值在其中。然后查看创建记录时执行的SQL语句,看看是否可以看到任何内容。如果无法访问attr_,那么我猜mailboxer gem正在专门针对某些字段进行创建或保存。
Notification Load (0.1ms) SELECT "notifications".* FROM "notifications" ORDER BY "notifications"."id" DESC LIMIT 1
--- !ruby/object:Message
attributes:
id: 4
type: Message
body: game
subject: wiz
lat: !!null
long: !!null
sender_id: 1
sender_type: User
conversation_id: 2
draft: false
updated_at: 2013-03-10 04:37:54.984277000Z
created_at: 2013-03-10 04:37:54.984277000Z
notified_object_id: !!null
notified_object_type: !!null
notification_code: !!null
attachment: !!null
=> nil