Rails4.2新特性学习

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


  • 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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值