Rails ActiveRecord 忽略参数并保存零数据

发布于 2025-01-08 15:09:46 字数 1744 浏览 3 评论 0原文

这难倒了我;由于某种原因,数据库使用 nil 字段而不是我的参数保存记录。谁能帮助我理解为什么 ActiveRecord 不使用我的参数?

数据库迁移:

class CreateRoutes < ActiveRecord::Migration
  def change
    create_table :routes do |t|
      t.integer :user_id
      t.string  :start_address
      t.string  :end_address
      t.text    :waypoints
      t.text    :schedule
      t.integer :duration

      t.timestamps
    end
    add_index :routes, :user_id
  end
end

route.rb:

class Route < ActiveRecord::Base
  attr_accessor :start_address, :end_address, :waypoints, :schedule, :duration
  belongs_to :user
  #serialize :waypoints, :schedule

  validates :user_id, presence: true
  validates :start_address, presence: true
  validates :end_address, presence: true
  validates :schedule, presence: true
  validates :duration, presence: true, numericality: { only_integer: true, greater_than: 0 }

end

routes_controller.rb:

class RoutesController < ApplicationController
  .
  .
  .
  def create
    logger.debug "\n\n*** #{params[:route]} ***"

    @route = current_user.routes.build(params[:route])

    logger.debug "*** The route is #{@route.inspect} ***\n\n"

    if @route.save
      flash[:success] = "Route saved!"
      redirect_to user_path(current_user)
    else
      render 'new'
    end
  end
  .
  .
  .
end

记录器输出:

*** {"start_address"=>"123 Sample St.", "end_address"=>"321 Elpmas St.", "waypoints"=>"None", "schedule"=>"Mondays", "duration"=>"15"} ***
*** The route is #<Route id: nil, user_id: 1, start_address: nil, end_address: nil, waypoints: nil, schedule: nil, duration: nil, created_at: nil, updated_at: nil> ***

This is stumping me; For some reason the db is saving the record with nil fields instead of my params. Can anyone help me understand why ActiveRecord isn't using my params?

db migration:

class CreateRoutes < ActiveRecord::Migration
  def change
    create_table :routes do |t|
      t.integer :user_id
      t.string  :start_address
      t.string  :end_address
      t.text    :waypoints
      t.text    :schedule
      t.integer :duration

      t.timestamps
    end
    add_index :routes, :user_id
  end
end

route.rb:

class Route < ActiveRecord::Base
  attr_accessor :start_address, :end_address, :waypoints, :schedule, :duration
  belongs_to :user
  #serialize :waypoints, :schedule

  validates :user_id, presence: true
  validates :start_address, presence: true
  validates :end_address, presence: true
  validates :schedule, presence: true
  validates :duration, presence: true, numericality: { only_integer: true, greater_than: 0 }

end

routes_controller.rb:

class RoutesController < ApplicationController
  .
  .
  .
  def create
    logger.debug "\n\n*** #{params[:route]} ***"

    @route = current_user.routes.build(params[:route])

    logger.debug "*** The route is #{@route.inspect} ***\n\n"

    if @route.save
      flash[:success] = "Route saved!"
      redirect_to user_path(current_user)
    else
      render 'new'
    end
  end
  .
  .
  .
end

logger output:

*** {"start_address"=>"123 Sample St.", "end_address"=>"321 Elpmas St.", "waypoints"=>"None", "schedule"=>"Mondays", "duration"=>"15"} ***
*** The route is #<Route id: nil, user_id: 1, start_address: nil, end_address: nil, waypoints: nil, schedule: nil, duration: nil, created_at: nil, updated_at: nil> ***

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稳稳的幸福 2025-01-15 15:09:46

attr_accessor 将覆盖由 ActiveRecord 生成的访问器,导致它们无法持久保存在数据库中——它们将像普通的旧式 Ruby 属性/成员,而不是ActiveRecord 的元编程魔力。

不过,数据库属性(持久属性)可以具有诸如 attr_accessible 之类的内容。

The attr_accessors will overwrite the accessors generated by ActiveRecord, causing them to not be persisted in the DB--they'll be like plain old Ruby properties/members instead of the meta-programmed magic of ActiveRecord.

DB properties (persistent properties) can have things like attr_accessible, though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文