属性保存在开发中但不在生产中

问题描述 投票:9回答:2

我有以下帮助方法:

def parse_potential_followers(params)
  t_id = TestSet.where(:test_name => params[:test_set][:test_name]).pluck(:id)[0].to_i
  screen_names = params[:potential_followers].first[1].split("\n").reject(&:blank?)
  screen_names.each do |s|
    potential_follower = PotentialFollower.new(
      :screen_name => s,
      :test_sets_id => t_id,
      :status => 'new',
      :slug => generate_slug([t_id.to_s, s])
    )
    potential_follower.save
  end
end

问题是,当我调用此方法时,如果在开发环境中的表中插入数据,则跳过test_sets_id,而不是在生产环境中。 其他三个属性保存得很好。

所有属性都在potential_followers表中定义。

我还在potential_followers_controller.rb中的potential_follower_params方法中拥有所有属性:

def potential_follower_params
  params.require(:potential_follower).permit(:screen_name, :test_sets_id, :connections, :status,
    :slug, :created_at, :updated_at)
end

test_sets_id定义为表中的整数。 我甚至尝试过编码t_id的值:

t_id = 12

但它仍然不适用于生产。

这是models / potential_follower.rb中的内容:

class PotentialFollower < ActiveRecord::Base
  belongs_to :TestSet
end

这是test_sets_contoller.rb中的方法:

def create
    @test_set = TestSet.new(test_set_params)
    respond_to do |format|
        if @test_set.save
            parse_potential_followers(params)
            format.html { redirect_to @test_set, notice: 'Test set was successfully created.' }
            format.json { render :show, status: :created, location: @test_set }
        else
            format.html { render :new }
            format.json { render json: @test_set.errors, status: :unprocessable_entity }
        end
    end
end

有任何想法吗?

ruby-on-rails ruby-on-rails-4 activerecord ruby-on-rails-4.2 ruby-2.1.5
2个回答
1
投票

生产数据库可能没有字段test_sets_id ,但在生产模式下,rails仍会创建数据库记录,而忽略哈希的test_sets_id字段。 rake db:migrate RAILS_ENV=production应解决问题。


1
投票

你正在偏离Rails惯例。 belongs_to应该是蛇形和单数形式,即:

belongs_to :test_set

数据库列也应该是单数。 因此,该列应重命名为test_set_id

belongs_to :test_set声明将做的是它将在PotentialFollower上生成test_set_id= (和test_set=方法)。 这是Rails的惯例。 一旦更改了belongs_to,它现在应该成功地将值保存在开发和生产中。

http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-belongs_to

© www.soinside.com 2019 - 2024. All rights reserved.