代码之家  ›  专栏  ›  技术社区  ›  vinhboy

rails activerecord属性=

  •  1
  • vinhboy  · 技术社区  · 16 年前

    我正在做:

    @person.attributes = params[:person]
    

    有人能告诉我为什么做attributes=会导致一堆sql显示在我的日志中吗?

    我看到一些精选的甚至是更新的。

    我发誓我不是在做“保存”——所以我不知道为什么设置属性会触发查询。

    是否有我丢失的前/后过滤器?

    谢谢。

    1 回复  |  直到 16 年前
        1
  •  3
  •   Ryan Bigg Andrés Bonilla    16 年前

    在标准模型上设置属性不会导致 SELECT 或 UPDATE 据我所知打电话来。当我与 脚本/控制台 会议:

    >> f = Forum.first
    
    ==> ./log/development.log <==
      SQL (0.1ms)   SET SQL_AUTO_IS_NULL=0
      Forum Load (0.4ms)   SELECT * FROM `forums` ORDER BY title asc LIMIT 1
      Forum Columns (12.6ms)   SHOW FIELDS FROM `forums`
    => #<Forum id: 1, title: "Welcome to rBoard!", description: "This is an example forum for Rboard.", is_visible_to_id: nil, topics_created_by_id: nil, position: 1, parent_id: nil, last_post_id: 1, last_post_forum_id: nil, topics_count: 1, posts_count: 4, category_id: nil, active: true, open: true>
    >> f.attributes
    => {"position"=>1, "is_visible_to_id"=>nil, "open"=>true, "topics_count"=>1, "title"=>"Welcome to rBoard!", "last_post_forum_id"=>nil, "posts_count"=>4, "id"=>1, "category_id"=>nil, "parent_id"=>nil, "topics_created_by_id"=>nil, "last_post_id"=>1, "description"=>"This is an example forum for Rboard.", "active"=>true}
    >> f.attributes = _
    => {"position"=>1, "is_visible_to_id"=>nil, "open"=>true, "topics_count"=>1, "title"=>"Welcome to rBoard!", "last_post_forum_id"=>nil, "posts_count"=>4, "id"=>1, "category_id"=>nil, "parent_id"=>nil, "topics_created_by_id"=>nil, "last_post_id"=>1, "description"=>"This is an example forum for Rboard.", "active"=>true}
    

    在这里可以看到,只有两个sql查询被执行,一个用来获取 Forum 记录并另一个查找 forums 表。

    直到我保存它,它才执行一些查询:

    >> f.attributes
    => {"position"=>1, "is_visible_to_id"=>nil, "open"=>true, "topics_count"=>1, "title"=>"Welcome to rBoard!", "last_post_forum_id"=>nil, "posts_count"=>4, "id"=>1, "category_id"=>nil, "parent_id"=>nil, "topics_created_by_id"=>nil, "last_post_id"=>1, "description"=>"This is an example forum for Rboard.", "active"=>true}
    >> attr = _
    => {"position"=>1, "is_visible_to_id"=>nil, "open"=>true, "topics_count"=>1, "title"=>"Welcome to rBoard!", "last_post_forum_id"=>nil, "posts_count"=>4, "id"=>1, "category_id"=>nil, "parent_id"=>nil, "topics_created_by_id"=>nil, "last_post_id"=>1, "description"=>"This is an example forum for Rboard.", "active"=>true}
    >> attr["position"] = 2
    => 2
    >> f.save
      SQL (0.2ms)   BEGIN
    => true
    >>   SQL (0.2ms)   COMMIT
    f.attributes = attr
    => {"position"=>2, "is_visible_to_id"=>nil, "open"=>true, "topics_count"=>1, "title"=>"Welcome to rBoard!", "last_post_forum_id"=>nil, "posts_count"=>4, "id"=>1, "category_id"=>nil, "parent_id"=>nil, "topics_created_by_id"=>nil, "last_post_id"=>1, "description"=>"This is an example forum for Rboard.", "active"=>true}
    >> WARNING: Can't mass-assign these protected attributes: id
    f.save
      SQL (0.2ms)   BEGIN
      Forum Update (20.3ms)   UPDATE `forums` SET `position` = 2 WHERE `id` = 1
      SQL (27.2ms)   COMMIT
    => true
    
    推荐文章