Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails ActiveRecord保存:Rails 3.2.19和Rails 4.2.0的区别_Ruby On Rails_Ruby_Activerecord - Fatal编程技术网

Ruby on rails ActiveRecord保存:Rails 3.2.19和Rails 4.2.0的区别

Ruby on rails ActiveRecord保存:Rails 3.2.19和Rails 4.2.0的区别,ruby-on-rails,ruby,activerecord,Ruby On Rails,Ruby,Activerecord,我有两个相同的项目:Rails 3.2.19和Rails 4.2.0beta4。 当我尝试做Foo.create时在Rails 3.2和Rails 4.2中,我在Rails控制台日志中看到了不同的查询 Rails 3.2输出: INSERT INTO `foos` (`created_at`, `updated_at`, `column_1`,`column_2`, ...) VALUES ('2014-11-19 11:20:28.391649', '2014-11-19 11:20:28.3

我有两个相同的项目:Rails 3.2.19和Rails 4.2.0beta4。 当我尝试做
Foo.create时在Rails 3.2和Rails 4.2中,我在Rails控制台日志中看到了不同的查询

Rails 3.2输出:

INSERT INTO `foos` (`created_at`, `updated_at`, `column_1`,`column_2`, ...) VALUES ('2014-11-19 11:20:28.391649', '2014-11-19 11:20:28.391649', NULL, NULL, ...)
INSERT INTO `foos` (`created_at`, `updated_at`) VALUES ('2014-11-19 11:20:28.391649', 545662, '2014-11-19 11:20:28.391649')
Rails 4.2输出:

INSERT INTO `foos` (`created_at`, `updated_at`, `column_1`,`column_2`, ...) VALUES ('2014-11-19 11:20:28.391649', '2014-11-19 11:20:28.391649', NULL, NULL, ...)
INSERT INTO `foos` (`created_at`, `updated_at`) VALUES ('2014-11-19 11:20:28.391649', 545662, '2014-11-19 11:20:28.391649')
请解释为什么不同的Rails版本会执行不同的查询,以及为什么会执行不同的查询?

这与4.x版本中默认启用的查询有关。 以文档中的示例为例:

class PeopleController < ActionController::Base
  # This will raise an ActiveModel::ForbiddenAttributes exception because it's using mass assignment
  # without an explicit permit step.
  def create
    Person.create(params[:person])
  end

  # This will pass with flying colors as long as there's a person key in the parameters, otherwise
  # it'll raise an ActionController::ParameterMissing exception, which will get caught by
  # ActionController::Base and turned into that 400 Bad Request reply.
  def update
    person = current_account.people.find(params[:id])
    person.update_attributes!(person_params)
    redirect_to person
  end

  private
    # Using a private method to encapsulate the permissible parameters is just a good pattern
    # since you'll be able to reuse the same permit list between create and update. Also, you
    # can specialize this method with per-user checking of permissible attributes.
    def person_params
      params.require(:person).permit(:name, :age)
    end
end
class PeopleController

person\u params
方法中,您需要允许更改所有要更改的参数。

因为他们更改了代码以提高效率。你可以找到它,但这不是一个真正的问题。