美文网首页
rails要绕过的坑

rails要绕过的坑

作者: SecondRocker | 来源:发表于2016-04-28 21:16 被阅读99次
    • 使用_helper方法构建html
      使用其他语言有些复杂的html需要拼字符串构建,这样不仅容易导致xss漏洞,而且没有语法高亮的代码难于阅读,在helper方法中你可以使用诸如link_to,content_tag等方法,让代码优美简洁
    • and,or 关键字
      and 、or不等同于 &&、||,他的优先级要低于 && 、||,低于赋值的=
    • migration 必须添加数据库约束
      rails的validate不一定能完全防范不合要求的数据,添加数据库约束能够起到最后一堵墙的作用
    • ActiveRecord的延迟加载
      ActiveRecord的has_many关联和scope很相似,都用了delegation,rails 2.3.x中返回的是namedscopes或associations。 虽然返回的是array,但是在对返回的associations或namedscopes调用方法时候,会根据不同的方法,delegate到不同的对象。调用:scopes, :with_scope, :scoped_methods等时继续返回scope 调用除了[nil? send object_id class extend find size count sum average maximum minimum paginate first last empty? any? respond_to?]中的方法时,先调用.all方法进行db操作。 即: user.posts.each{...} == user.posts.all.each{...} .这些方法都仍然返回一个Relation对象。直到调用map/each等方法才真正的进行数据库操作。

    相关文章

      网友评论

          本文标题:rails要绕过的坑

          本文链接:https://www.haomeiwen.com/subject/dpfvlttx.html