小编Hia*_*nho的帖子

如何将序列添加到迁移并在模型中使用它们?

我想要一个Customer带有普通主键的模型和另一个用于存储自定义"客户编号"的列.另外,我希望db能够处理默认的客户编号.我认为,定义序列是最好的方法.我使用PostgreSQL.看看我的迁移:

class CreateAccountsCustomers < ActiveRecord::Migration
  def up

    say "Creating sequenze for customer number starting at 1002"
    execute 'CREATE SEQUENCE customer_no_seq START 1002;'

    create_table :accounts_customers do |t|
      t.string :type
      t.integer :customer_no, :unique => true
      t.integer :salutation, :limit => 1
      t.string :cp_name_1
      t.string :cp_name_2
      t.string :cp_name_3
      t.string :cp_name_4
      t.string :name_first, :limit => 55
      t.string :name_last, :limit => 55
      t.timestamps
    end

    say "Adding NEXTVAL('customer_no_seq') to column cust_id"
    execute "ALTER TABLE accounts_customers ALTER COLUMN customer_no SET DEFAULT NEXTVAL('customer_no_seq');"

  end

  def down
    drop_table …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails rails-models rails-migrations rails-postgresql ruby-on-rails-3.1

18
推荐指数
1
解决办法
6646
查看次数

Rails 4.0中的路由,路径助手和STI

这真让我抓狂!我有两个型号LionCheetah.两者都继承自Wildcat.

class Wildcat < ActiveRecord::Base; end
class Lion < Wildcat; end
class Cheetah < Wildcat; end
Run Code Online (Sandbox Code Playgroud)

STI在这里使用.

它们都通过控制器处理WildcatsController.在那里,我有一个before_filer得到type野猫从params[:type]和所有其他的东西来使用正确的类.

在我routes.rb,我创建了以下路线:

resources :lions, controller: 'wildcats', type: 'Lion'
resources :cheetahs, controller: 'wildcats', type: 'Cheetah'
Run Code Online (Sandbox Code Playgroud)

如果我现在想用的路径帮手,我从路线(得到lions_path,lion_path,new_lion_path,等),一切工作正常,除了shownew路径.例如,lions_path返回路径/lions.该new路径返回/lions/new?type=Lion.与show路径相同.当我尝试进入/lions/new我的根域时,它正确地在后台添加了类型参数.

所以,我的问题是,type如果我使用路径助手,为什么Rails会将参数添加到url?为什么只为newshow

我正在使用一个新的Rails应用程序使用Ruby 2.0运行Rails 4.0.0.

ruby routes ruby-on-rails sti ruby-on-rails-4

7
推荐指数
1
解决办法
2420
查看次数