Rails save 总是保存不上 大家帮看一下代码 (结帖)

tiseheaini · 2012年11月23日 · 最后由 woaigithub 回复于 2012年11月23日 · 3653 次阅读

有一段代码好像不工作了,大家帮看看

def likeable
    @topic = Topic.last
    ## likeable 是一个字符串 类似 "127.0.0.1 127.0.0.2 "
    @topicarr = @topic.likeable.split

    ## 就是这段代码有问题 ip 无法插入到数据库字段里
    @topic.likeable << request.remote_ip.to_s + ' ' 
    render :text => @topic.likeable.split.length if @topic.save
    end
  end

rails server 中的 log

Started GET "/topics/likeable" for 127.0.0.1 at 2012-11-23 13:33:45 +0800
Processing by TopicsController#likeable as HTML
  Topic Load (5.7ms)  SELECT `topics`.* FROM `topics` ORDER BY `topics`.`id` DESC LIMIT 1
   (0.8ms)  BEGIN
   (3.5ms)  COMMIT
  Rendered text template (0.0ms)
Completed 200 OK in 14ms (Views: 0.7ms | ActiveRecord: 10.0ms)

看 log 输出好像没有保存的过程

1.9.3p125 :042 > Topic.last.likeable
 => ""

大家帮忙看看

render :text => @topic.likeable.split.length if @topic.save 改成 if @topic.save render :text => @topic.likeable.split.length else render :text => @topic.errors end 看看

#1 楼 @ywencn 没有输出错误 数据库还是没插入成功 不过 log 有一条错误

Started GET "/topics/likeable" for 127.0.0.1 at 2012-11-23 14:07:52 +0800
Processing by TopicsController#likeable as HTML
  Topic Load (3.2ms)  SELECT `topics`.* FROM `topics` ORDER BY `topics`.`id` DESC LIMIT 1
   (2.6ms)  BEGIN
   (1.4ms)  COMMIT
  Rendered text template (0.0ms)
Completed 200 OK in 11ms (Views: 0.7ms | ActiveRecord: 7.2ms)
[2012-11-23 14:07:52] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true

#1 楼 @ywencn 数据库里没有插入这条记录 @topic.likeable 没有变化,看 log 信息好像也没有插入这条记录??

#1 楼 @ywencn

1.9.3p125 :016 > t = Topic.last
  Topic Load (6.5ms)  SELECT `topics`.* FROM `topics` ORDER BY `topics`.`id` DESC LIMIT 1
 => #<Topic id: 1, body: "<span style=\"color:#333333;font-size:14px;backgroun...", likeable: "127.0.0.1 127.0.0.1 ", unlikeable: "", timeint: 201211237, created_at: "2012-11-23 05:02:23", updated_at: "2012-11-23 05:24:57"> 
1.9.3p125 :017 > t.likeable << "127.0.0.2" + " "
 => "127.0.0.1 127.0.0.1 127.0.0.2 " 
1.9.3p125 :018 > t.likeable
 => "127.0.0.1 127.0.0.1 127.0.0.2 " 
1.9.3p125 :019 > t.save
   (0.2ms)  BEGIN
   (0.3ms)  COMMIT
 => true 
1.9.3p125 :020 > Topic.last.likeable
  Topic Load (15.8ms)  SELECT `topics`.* FROM `topics` ORDER BY `topics`.`id` DESC LIMIT 1
 => "127.0.0.1 127.0.0.1 " 
1.9.3p125 :021 > 

在 console 里面也是无法保存成功??

#1 楼 @ywencn

1.9.3p125 :016 > t = Topic.last
  Topic Load (6.5ms)  SELECT `topics`.* FROM `topics` ORDER BY `topics`.`id` DESC LIMIT 1
 => #<Topic id: 1, body: "<span style=\"color:#333333;font-size:14px;backgroun...", likeable: "127.0.0.1 127.0.0.1 ", unlikeable: "", timeint: 201211237, created_at: "2012-11-23 05:02:23", updated_at: "2012-11-23 05:24:57"> 
1.9.3p125 :017 > t.likeable << "127.0.0.2" + " "
 => "127.0.0.1 127.0.0.1 127.0.0.2 " 
1.9.3p125 :018 > t.likeable
 => "127.0.0.1 127.0.0.1 127.0.0.2 " 
1.9.3p125 :019 > t.save
   (0.2ms)  BEGIN
   (0.3ms)  COMMIT
 => true 
1.9.3p125 :020 > Topic.last.likeable
  Topic Load (15.8ms)  SELECT `topics`.* FROM `topics` ORDER BY `topics`.`id` DESC LIMIT 1
 => "127.0.0.1 127.0.0.1 " 
1.9.3p125 :021 > 

在 console 里面也是无法保存成功??

t.likeable="aaa"
t.save```

你没有更新字段吧。 你是第一次 new 还是后续的 update。 把 topic 的 model 贴出来。

#7 楼 @woaigithub @topic.likeable << request.remote_ip.to_s + ' ' 这一行代码 就是更新字段

用 save!试试

#9 楼 @cantin 这个方法好坏呀!不过我试过了,也不行 我在写文章的 顶/踩 功能,在 likeable 字段保存用户的 ip 地址 防止重复点击。

save!没报异常?likeable 是 store 存的吗?

#1 楼 @ywencn #6 楼 @woaigithub #9 楼 @cantin 问题解决了,虽然不知道问题出在哪里了 解决方法 开始时 likeable 字段是 string 后来换成了 text 代码也跟着改了

def likeable                 
    @topic = Topic.last        
    @topicarr = @topic.likeable.to_s.split                                                                                    

    @topic.likeable = @topic.likeable.to_s + request.remote_ip.to_s + ' '                                                     
    if @topic.save             
      render :text => @topic.likeable.split.length                                                                            
    else
      render :text => @topic.errors 
    end
  end

感觉没有问题啊,你试试 不用“<<”而采用 t.likeable = t.likeable+t.likeable+request.remote_ip.to_s,然后再 t.save

#13 楼 @Roam 我感觉也没有问题,可就是保存不上,刚才改了,好像可以了,难道这是个坑

似乎真的是个坑,打开 rails console ,试试这样:

t.likeable << '127.0.0.1'
t.likeable_changed?

我这边返回的是个 false,猜想是这个原因导致 t.save 的时候没有生成任何 UPDATE 语句 大牛们 @fredwu @luicore @saito @huacnlee 可以去提 issue 和 pull request 了

#15 楼 @dotnil

这种 in-place 的写法是会导致这个问题的,Rails 上已经有注释说明了

https://github.com/rails/rails/blob/master/activemodel/lib/active_model/dirty.rb#L81-88

你的topic的 model 有哪些验证啊? #14 楼 @tiseheaini

#16 楼 @HungYuHei 原来如此!我还记得看 Agile 那本书的时候看到过这个的,忘记这茬了

#16 楼 @HungYuHei 犀利,没注意过这个细节。坑,mark 了,以后慎用!

恭喜恭喜自己找到答案了

需要 登录 后方可回复, 如果你还没有账号请 注册新账号