小编rai*_*ner的帖子

如何生成迁移以使引用具有多态性

我有一个Products表,想要添加一列:

t.references :imageable, :polymorphic => true
Run Code Online (Sandbox Code Playgroud)

我试图通过以下方式为此生成迁移:

$ rails generate migration AddImageableToProducts imageable:references:polymorphic
Run Code Online (Sandbox Code Playgroud)

但我显然做错了.任何人都可以提出任何建议吗?谢谢

当我在生成迁移后尝试手动将其放入时,我这样做:

class AddImageableToProducts < ActiveRecord::Migration
  def self.up
    add_column :products, :imageable, :references, :polymorphic => true
  end

  def self.down
    remove_column :products, :imageable
  end
end
Run Code Online (Sandbox Code Playgroud)

它仍然没有奏效

ruby-on-rails polymorphic-associations rails-migrations

116
推荐指数
4
解决办法
6万
查看次数

Rails验证可防止保存

我有这样的用户模型:

class User < ActiveRecord::Base
    validates :password, :presence => true,
                         :confirmation => true,
                         :length => { :within => 6..40 }
    .
    .
    .
end
Run Code Online (Sandbox Code Playgroud)

在User模型中,我有一个我想要从OrdersController保存的billing_id列,如下所示:

class OrdersController < ApplicationController
    .
    .
    .
    def create
        @order = Order.new(params[:order])
        if @order.save
            if @order.purchase
                response = GATEWAY.store(credit_card, options)
                result = response.params['billingid']
                @thisuser = User.find(current_user)
                @thisuser.billing_id  = result
                if @thisuser.save
                        redirect_to(root_url), :notice => 'billing id saved')
                    else
                        redirect_to(root_url), :notice => @thisuser.errors)
                    end
            end
        end
    end
Run Code Online (Sandbox Code Playgroud)

因为validates :password在User模型中,@thisuser.save不保存.但是,一旦我注释掉验证,@thisuser.save返回true.这对我来说是一个陌生的领域,因为我认为这个验证仅在创建新用户时有效.有人可以告诉我validates …

validation ruby-on-rails

3
推荐指数
1
解决办法
1838
查看次数