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 阅读(1208) 评论(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热点博客

好友博客

搜索

  •  

最新评论

阅读排行榜

评论排行榜