我对 RoR 中的 Mailboxer 有疑问。我按照官方指南安装 Mailboxer gem。
添加到您的 Gemfile:
gem 'mailboxer'
然后运行:
$ bundle install
运行安装脚本:
$ rails g mailboxer:install
并且不要忘记迁移您的数据库:
$ rake db:migrate
但
== CreateMailboxer: migrating ================================================
-- create_table(:mailboxer_conversations)
-> 0.0495s
-- create_table(:mailboxer_receipts)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
wrong number of arguments (4 for 2).../vendor/cache/ruby/1.9.1/gems/foreigner-1.7.4/lib/foreigner/connection_adapters/abstract/table_definition.rb:4:in `foreign_key'
.../vendor/cache/ruby/1.9.1/gems/schema_plus-1.8.9/lib/schema_plus/active_record/connection_adapters/table_definition.rb:171:in `add_foreign_key'
.../vendor/cache/ruby/1.9.1/gems/schema_plus-1.8.9/lib/schema_plus/active_record/column_options_handler.rb:44:in `schema_plus_handle_column_options'
.../vendor/cache/ruby/1.9.1/gems/schema_plus-1.8.9/lib/schema_plus/active_record/connection_adapters/table_definition.rb:137:in `column_with_schema_plus'
.../db/migrate/20150623102808_create_mailboxer.mailboxer_engine.rb:14:in `block in up'
.../vendor/cache/ruby/1.9.1/gems/foreigner-1.7.4/lib/foreigner/connection_adapters/abstract/schema_statements.rb:16:in `call'
.../vendor/cache/ruby/1.9.1/gems/foreigner-1.7.4/lib/foreigner/connection_adapters/abstract/schema_statements.rb:16:in `block in create_table'
.../vendor/cache/ruby/1.9.1/gems/schema_plus-1.8.9/lib/schema_plus/active_record/connection_adapters/schema_statements.rb:44:in `block in create_table_with_schema_plus'
.../vendor/cache/ruby/1.9.1/gems/activerecord-3.2.22/lib/active_record/connection_adapters/abstract/schema_statements.rb:160:in `create_table'
.../vendor/cache/ruby/1.9.1/gems/schema_plus-1.8.9/lib/schema_plus/active_record/connection_adapters/schema_statements.rb:39:in `create_table_with_schema_plus'
.../vendor/cache/ruby/1.9.1/gems/foreigner-1.7.4/lib/foreigner/connection_adapters/abstract/schema_statements.rb:14:in `create_table'
.../vendor/cache/ruby/1.9.1/gems/activerecord-3.2.22/lib/active_record/migration.rb:466:in `block in method_missing'
.../vendor/cache/ruby/1.9.1/gems/activerecord-3.2.22/lib/active_record/migration.rb:438:in `block in say_with_time'
.../vendor/cache/ruby/1.9.1/gems/activerecord-3.2.22/lib/active_record/migration.rb:438:in `say_with_time'
.../vendor/cache/ruby/1.9.1/gems/activerecord-3.2.22/lib/active_record/migration.rb:458:in `method_missing'
.../vendor/cache/ruby/1.9.1/gems/activerecord-3.2.22/lib/active_record/migration.rb:334:in `method_missing'
.../db/migrate/20150623102808_create_mailboxer.mailboxer_engine.rb:12:in `up'
.../vendor/cache/ruby/1.9.1/gems/activerecord-3.2.22/lib/active_record/migration.rb:370:in `up'
.../vendor/cache/ruby/1.9.1/gems/activerecord-3.2.22/lib/active_record/migration.rb:410:in `block (2 levels) in migrate'
.../vendor/cache/ruby/1.9.1/gems/activerecord-3.2.22/lib/active_record/migration.rb:410:in `block in migrate'
.../vendor/cache/ruby/1.9.1/gems/activerecord-3.2.22/lib/active_record/connection_adapters/abstract/connection_pool.rb:129:in `with_connection'
.../vendor/cache/ruby/1.9.1/gems/activerecord-3.2.22/lib/active_record/migration.rb:389:in `migrate'
.../vendor/cache/ruby/1.9.1/gems/activerecord-3.2.22/lib/active_record/migration.rb:528:in `migrate'
.../vendor/cache/ruby/1.9.1/gems/activerecord-3.2.22/lib/active_record/migration.rb:720:in `block (2 levels) in migrate'
.../vendor/cache/ruby/1.9.1/gems/activerecord-3.2.22/lib/active_record/migration.rb:775:in `call'
.../vendor/cache/ruby/1.9.1/gems/activerecord-3.2.22/lib/active_record/migration.rb:775:in `block in ddl_transaction'
.../vendor/cache/ruby/1.9.1/gems/activerecord-3.2.22/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
.../vendor/cache/ruby/1.9.1/gems/activerecord-3.2.22/lib/active_record/transactions.rb:208:in `transaction'
.../vendor/cache/ruby/1.9.1/gems/activerecord-3.2.22/lib/active_record/migration.rb:775:in `ddl_transaction'
.../vendor/cache/ruby/1.9.1/gems/activerecord-3.2.22/lib/active_record/migration.rb:719:in `block in migrate'
.../vendor/cache/ruby/1.9.1/gems/activerecord-3.2.22/lib/active_record/migration.rb:700:in `each'
.../vendor/cache/ruby/1.9.1/gems/activerecord-3.2.22/lib/active_record/migration.rb:700:in `migrate'
.../vendor/cache/ruby/1.9.1/gems/activerecord-3.2.22/lib/active_record/migration.rb:570:in `up'
.../vendor/cache/ruby/1.9.1/gems/activerecord-3.2.22/lib/active_record/migration.rb:551:in `migrate'
.../vendor/cache/ruby/1.9.1/gems/activerecord-3.2.22/lib/active_record/railties/databases.rake:193:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:migrate
我使用: mailboxer (0.12.5) rake (10.4.2) rails (3.2.22) ruby (1.9.3p547)
生成的文件rails g mailboxer:install
有:
# This migration comes from mailboxer_engine (originally 20110511145103)
class CreateMailboxer < ActiveRecord::Migration
def self.up
#Tables
#Conversations
create_table :mailboxer_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 :mailboxer_receipts do |t|
t.references :receiver, :polymorphic => true
t.column :notification_id, :integer, :null => false
t.column :is_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 :mailboxer_notifications do |t|
t.column :type, :string
t.column :body, :text
t.column :subject, :string, :default => ""
t.references :sender, :polymorphic => true
t.column :conversation_id, :integer
t.column :draft, :boolean, :default => false
t.string :notification_code, :default => nil
t.references :notified_object, :polymorphic => true
t.column :attachment, :string
t.column :updated_at, :datetime, :null => false
t.column :created_at, :datetime, :null => false
t.boolean :global, default: false
t.datetime :expires
end
#Indexes
#Conversations
#Receipts
add_index "mailboxer_receipts","notification_id"
#Messages
add_index "mailboxer_notifications","conversation_id"
#Foreign keys
#Conversations
#Receipts
add_foreign_key "mailboxer_receipts", "mailboxer_notifications", :name => "receipts_on_notification_id", :column => "notification_id"
#Messages
add_foreign_key "mailboxer_notifications", "mailboxer_conversations", :name => "notifications_on_conversation_id", :column => "conversation_id"
end
def self.down
#Tables
remove_foreign_key "mailboxer_receipts", :name => "receipts_on_notification_id"
remove_foreign_key "mailboxer_notifications", :name => "notifications_on_conversation_id"
#Indexes
drop_table :mailboxer_receipts
drop_table :mailboxer_conversations
drop_table :mailboxer_notifications
end
end
# This migration comes from mailboxer_engine (originally 20131206080416)
class AddConversationOptout < ActiveRecord::Migration
def self.up
create_table :mailboxer_conversation_opt_outs do |t|
t.references :unsubscriber, :polymorphic => true
t.references :conversation
end
add_foreign_key "mailboxer_conversation_opt_outs", "mailboxer_conversations", :name => "mb_opt_outs_on_conversations_id", :column => "conversation_id"
end
def self.down
remove_foreign_key "mailboxer_conversation_opt_outs", :name => "mb_opt_outs_on_conversations_id"
drop_table :mailboxer_conversation_opt_outs
end
end
# This migration comes from mailboxer_engine (originally 20131206080417)
class AddMissingIndices < ActiveRecord::Migration
def change
# We'll explicitly specify its name, as the auto-generated name is too long and exceeds 63
# characters limitation.
add_index :mailboxer_conversation_opt_outs, [:unsubscriber_id, :unsubscriber_type],
name: 'index_mailboxer_conversation_opt_outs_on_unsubscriber_id_type'
add_index :mailboxer_conversation_opt_outs, :conversation_id
add_index :mailboxer_notifications, :type
add_index :mailboxer_notifications, [:sender_id, :sender_type]
# We'll explicitly specify its name, as the auto-generated name is too long and exceeds 63
# characters limitation.
add_index :mailboxer_notifications, [:notified_object_id, :notified_object_type],
name: 'index_mailboxer_notifications_on_notified_object_id_and_type'
add_index :mailboxer_receipts, [:receiver_id, :receiver_type]
end
end
谢谢!