Rails 4.2正式版已早2014年12月19日发布,该版本加入了一些新特性,就让我们来看看一些主要特性吧。
$ bin/rails generate job guests_cleanup
invoke test_unit
create test/jobs/guests_cleanup_job_test.rb
create app/jobs/guests_cleanup_job.rb
- 将后台任务加入到队列中
<pre name="code" class="ruby"><pre name="code" class="ruby"># app/jobs/guests_cleanup_job.rb
class GuestsCleanupJob < ActiveJob::Base
queue_as :default
def perform(*args)
# Do something later
end
end
# In your controller action or model or test in the console
# Enqueue a job to be performed as soon the queueing system is free.
GuestsCleanupJob.perform_later record
# Enqueue a job to be performed tomorrow at noon.
GuestsCleanupJob.set(wait_until: Date.tomorrow.noon).perform_later(record)
# Enqueue a job to be performed 1 week from now.
GuestsCleanupJob.set(wait: 1.week).perform_later(record)
- 设置后台执行任务的适配器
# config/application.rb
module YourApp
class Application < Rails::Application
# Be sure to have the adapter's gem in your Gemfile and follow
# the adapter's specific installation and deployment instructions.
config.active_job.queue_adapter = :sidekiq
end
end
- 异步发送邮件
$ bin/rails generate mailer UserMailer
create app/mailers/user_mailer.rb
create app/mailers/application_mailer.rb
invoke erb
create app/views/user_mailer
create app/views/layouts/mailer.text.erb
create app/views/layouts/mailer.html.erb
invoke test_unit
create test/mailers/user_mailer_test.rb
create test/mailers/previews/user_mailer_preview.rb
# app/mailers/application_mailer.rb
class ApplicationMailer < ActionMailer::Base
default "from@example.com"
layout 'mailer'
end
# app/mailers/user_mailer.rb
class UserMailer < ApplicationMailer
end
# In your controller action or model or test in the console
# If you want to send the email now use #deliver_now
UserMailer.welcome(@user).deliver_now
# If you want to send the email through Active Job use #deliver_later
UserMailer.welcome(@user).deliver_later
- Adequate Record:通过缓存数据来加速ActiveRecord 2x
- Web Console
# Gemfile
# Access an IRB console on exception pages or by using <%= console %> in views
gem 'web-console', '~> 2.0'
- Foreign Keys:添加了add_foreign_key和remove_foreign_key方法
# Creating a simple foreign key
add_foreign_key :articles, :authors
# generates:
# ALTER TABLE "articles" ADD CONSTRAINT articles_author_id_fk FOREIGN KEY ("author_id") REFERENCES "authors" ("id")
# Creating a foreign key on a specific column
add_foreign_key :articles, :users, column: :author_id, primary_key: "lng_id"
# generates:
# ALTER TABLE "articles" ADD CONSTRAINT fk_rails_58ca3d3a82 FOREIGN KEY ("author_id") REFERENCES "users" ("lng_id")
# Creating a cascading foreign key
add_foreign_key :articles, :authors, on_delete: :cascade
# generates:
# ALTER TABLE "articles" ADD CONSTRAINT articles_author_id_fk FOREIGN KEY ("author_id") REFERENCES "authors" ("id") ON DELETE CASCADE
# Removes the foreign key on accounts.branch_id.
remove_foreign_key :accounts, :branches
# Removes the foreign key on accounts.owner_id.
remove_foreign_key :accounts, column: :owner_id
# Removes the foreign key named special_fk_name on the accounts table.
remove_foreign_key :accounts, name: :special_fk_name