Create将属性转换为nil,update不生成sql update语句

时间:2012-01-25 00:00:44

标签: activerecord model ruby-on-rails-3.1 controller

Rails 3.1 ruby 1.9.2p290(2011-07-09)[i386-mingw32] sqlite3的

我有一个名为usecase的模型,它没有正确创建或更新。当我创建新记录时,文本列设置为nil,并捕获基于选择值的整数值。在用户提供的输入字段中还有一个整数值,它设置为nil。

当我尝试更新时,日志显示没有创建或执行sql输入语句。我能够从控制台创建新记录并正确更新它们。

以下是型号代码(注意已删除所有验证):

class Usecase < ActiveRecord::Base
attr_accessible :due_at, :failure_end_condition, :frequency, :level_id, :number, :performance_target,
              :primary_actor_id, :priority_id, :profile_id, :project_id, :purpose, :success_end_condition,
              :title, :trigger, :usecase_status_id
has_many :actor_assignments
has_many :actors, :through => :actor_assignments
has_many :pre_conditions
has_many :post_conditions
has_many :usecase_as_conditions
has_many :scenarios
has_many :variations
belongs_to :profile
belongs_to :project
belongs_to :usecase_status

以下是控制器代码:class UsecasesController&lt; ApplicationController中

def new
  @usecase = Usecase.new
  @usecase.profile_id = current_user.profile.id
  @usecase.set_defaults(current_user.profile.default_project_id)
  respond_to do |format|
    format.html # new.html.erb
    format.json { render json: @usecase }
  end
end

def edit
  @usecase = Usecase.find(params[:id])
  respond_to do |format|
    format.html {render "edit"}
    format.json { render json: @usecase }
  end
end

def create
  @usecase = Usecase.new(params[:usecases])
  respond_to do |format|
    if @usecase.save
      format.html { redirect_to [:edit, @usecase], notice: 'Usecase was successfully created.' }
      format.json { render json: @usecase, status: :created, location: @usecase }
    else
      format.html { render action: "new" }
      format.json { render json: @usecase.errors, status: :unprocessable_entity }
    end
  end
end

def update
  @usecase = Usecase.find(params[:id])

  respond_to do |format|
    if @usecase.update_attributes(params[:usecases])
      format.html { redirect_to @usecase, notice: 'Usecase was successfully updated.' }
      format.json {  head :ok}
    else
      format.html { render action: "edit" }
      format.json { render json: @usecase.errors, status: :unprocessable_entity }
    end
  end
end

记录新记录插入(请注意,接收参数:title有一个值,但在insert语句中它是nil。

    Started POST "/usecases" for 127.0.0.1 at 2012-01-24 14:50:40 -0800
  Processing by UsecasesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"Uscho1j5FCsbdRUhTyg/Ji79yyPwyMRZUcyiWYtSw54=", 
    "usecase"=>{"number"=>"1", "title"=>"Create New Record", "due_at"=>"2012-02-07"}, "usecases"=>{"project_id"=>"2", 
                "usecase_status_id"=>"1", "profile_id"=>"3"}, "commit"=>"Create Usecase"}
  [1m[36mSQL (3.0ms)[0m  [1m
 INSERT INTO "usecases" ("created_at", "due_at", "failure_end_condition", "frequency", "level_id", "lock_version", 
                         "number", "performance_target", "primary_actor_id", "priority_id", "profile_id", "project_id", 
                         "purpose", "success_end_condition", "title", "trigger", "updated_at", "usecase_status_id") 
  VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)[0m  
 [["created_at", Tue, 24 Jan 2012 22:50:40 UTC +00:00], ["due_at", nil], ["failure_end_condition", nil], ["frequency", nil], ["level_id", nil], 
  ["lock_version", 0], ["number", nil], ["performance_target", nil], ["primary_actor_id", nil], ["priority_id", nil], ["profile_id", 3], 
  ["project_id", 2], ["purpose", nil], ["success_end_condition", nil], ["title", nil], ["trigger", nil], 
  ["updated_at", Tue, 24 Jan 2012 22:50:40 UTC +00:00], ["usecase_status_id", 1]]
Redirected to http://localhost:3000/usecases/2/edit
Completed 302 Found in 71ms

记录更新(注意没有创建UPDATE sql语句)

    Started PUT "/usecases/2" for 127.0.0.1 at 2012-01-24 14:57:04 -0800
  Processing by UsecasesController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"Uscho1j5FCsbdRUhTyg/Ji79yyPwyMRZUcyiWYtSw54=", "usecase"=>{"number"=>"1", "project_id"=>"2", 
               "title"=>"Update Row With New Title", "usecase_status_id"=>"1", "due_at"=>"2012-01-28", "profile_id"=>"3"}, "commit"=>"Update Usecase", "id"=>"2"}
  [1m[36mUsecase Load (0.0ms)[0m  [1mSELECT "usecases".* FROM "usecases" WHERE "usecases"."id" = ? LIMIT 1[0m  [["id", "2"]]
  [1m[35mProject Load (1.0ms)[0m  SELECT "projects".* FROM "projects" ORDER BY title ASC
  [1m[36mUsecaseStatus Load (1.0ms)[0m  [1mSELECT "usecase_statuses".* FROM "usecase_statuses" ORDER BY rank ASC[0m
  [1m[35mProfile Load (0.0ms)[0m  SELECT "profiles".* FROM "profiles" ORDER BY last_name ASC, first_name ASC
Rendered usecases/test.html.erb within layouts/application (147.0ms)
Rendered layouts/_stylesheets.html.erb (2.0ms)
  [1m[36mUser Load (0.0ms)[0m  [1mSELECT "users".* FROM "users" WHERE "users"."id" = 3 LIMIT 1[0m
  [1m[35mProfile Load (1.0ms)[0m  SELECT "profiles".* FROM "profiles" WHERE "profiles"."user_id" = 3 LIMIT 1
  [1m[36mAccount Load (0.0ms)[0m  [1mSELECT "accounts".* FROM "accounts" WHERE "accounts"."id" = 3 LIMIT 1[0m
Rendered layouts/_header.html.erb (330.0ms)
Rendered layouts/_footer.html.erb (0.0ms)
Completed 200 OK in 674ms (Views: 614.0ms | ActiveRecord: 7.0ms)

感谢您查看此内容。

1 个答案:

答案 0 :(得分:0)

不应该这样 @usecase = Usecase.new(params[:usecase])  和 if @usecase.update_attributes(params[:usecase]) 分别?

希望能解决它。