posts - 262,  comments - 221,  trackbacks - 0

          假如我們把attr_accessible設置成只有name是可以訪問的,然后在Console中執行如下命令:

          ruby-1.9.2-p180 :001 > user = User.create(:name => "Paul", :email => "pengpenglin@163.com")
           
          => #<User id: 1, name: "Paul", email: nil, created_at: "2011-09-03 04:42:59", updated_at: "2011-09-03 04:42:59"> 
          ruby-1.9.2-p180 :002 > user.email
           
          => nil 
          ruby-1.9.2-p180 :003 > user.email = "bob@163.com"
           
          => "bob@163.com" 
          ruby-1.9.2-p180 :004 > user.email
           
          => "bob@163.com" 
          ruby-1.9.2-p180 :005 > user.save
           
          => true 
          ruby-1.9.2-p180 :006 > user.reload.email
           
          => "bob@163.com" 
          ruby-1.9.2-p180 :007 > User.find(1)
           
          => #<User id: 1, name: "Paul", email: "bob@163.com", created_at: "2011-09-03 04:42:59", updated_at: "2011-09-03 04:43:31"> 

          不是not accessible 嗎?怎么email還是能夠改變?



          -------------------------------------------------------------
          生活就像打牌,不是要抓一手好牌,而是要盡力打好一手爛牌。
          posted on 2011-09-03 12:51 Paul Lin 閱讀(1244) 評論(1)  編輯  收藏 所屬分類: RoR


          FeedBack:
          # re: Rails的attr_accessible方法的奇怪特性
          2011-09-06 16:20 | g.zhen.ning
          Specifies a white list of model attributes that can be set via mass-assignment.

          User.create(:name => "Paul", :email => "pengpenglin@163.com")
          這就算是mass-assignment了,所以email是nil的,其后直接email =就不再是mass-assignment了。  回復  更多評論
            
          <2011年9月>
          28293031123
          45678910
          11121314151617
          18192021222324
          2526272829301
          2345678

          常用鏈接

          留言簿(21)

          隨筆分類

          隨筆檔案

          BlogJava熱點博客

          好友博客

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 咸丰县| 同心县| 扎囊县| 永德县| 镇平县| 安福县| 阿拉尔市| 宜川县| 和林格尔县| 寻甸| 通许县| 喜德县| 高雄县| 沈阳市| 德州市| 广元市| 连南| 永吉县| 齐河县| 高邮市| 宜昌市| 扎兰屯市| 庆元县| 林西县| 潞城市| 平定县| 泸西县| 新郑市| 德兴市| 凉山| 北流市| 长乐市| 石嘴山市| 平果县| 台北市| 文成县| 江门市| 富平县| 瓦房店市| 铜山县| 莱阳市|