如何在轨道中加载关联4

时间:2014-10-04 14:11:34

标签: ruby-on-rails activerecord ruby-on-rails-4 associations eager-loading

我正在尝试进行急切加载,因为现在我在rails服务器日志中看到了数百个针对组的查询。

我可以用

@links = Link.joins(:group)
             .where(@conditions)
             .references(:group)
             .order('groups.group_name, links.position')

所以我尝试将其更改为

@links = Link.includes(:group)
    .where(@conditions)
    .references(:group)
    .order('groups.group_name, links.position')

但我的日志(仍然)显示[样本]

  CACHE (0.0ms)  SELECT `groups`.* FROM `groups` WHERE `groups`.`id` = 6 ORDER BY `groups`.`group_name` ASC LIMIT 1  [["id", 6]]
  Rendered links/_content_toggle.html.haml (0.9ms)
  Rendered links/_link_toggle.html.haml (0.3ms)
  Rendered links/_content_toggle.html.haml (0.2ms)
  CACHE (0.0ms)  SELECT `groups`.* FROM `groups` WHERE `groups`.`id` = 6 ORDER BY `groups`.`group_name` ASC LIMIT 1  [["id", 6]]
  Rendered links/_content_toggle.html.haml (1.2ms)
  Rendered links/_content_toggle.html.haml (0.2ms)
  Rendered links/_content_toggle.html.haml (0.3ms)
  CACHE (0.1ms)  SELECT `groups`.* FROM `groups` WHERE `groups`.`id` = 6 ORDER BY `groups`.`group_name` ASC LIMIT 1  [["id", 6]]
  Rendered links/_content_toggle.html.haml (1.0ms)
  Rendered links/_content_toggle.html.haml (0.2ms)
  Rendered links/_link_toggle.html.haml (0.4ms)
  Rendered links/_content_toggle.html.haml (0.3ms)
  CACHE (0.0ms)  SELECT `groups`.* FROM `groups` WHERE `groups`.`id` = 6 ORDER BY `groups`.`group_name` ASC LIMIT 1  [["id", 6]]
  Rendered links/_content_toggle.html.haml (1.0ms)
  Rendered links/_link_toggle.html.haml (0.4ms)
  Rendered links/_content_toggle.html.haml (0.2ms)
  Group Load (0.3ms)  SELECT `groups`.* FROM `groups` WHERE `groups`.`id` = 13 ORDER BY `groups`.`group_name` ASC LIMIT 1
  Rendered links/_content_toggle.html.haml (0.9ms)
  Rendered links/_content_toggle.html.haml (0.3ms)
  Rendered links/_link_toggle.html.haml (0.4ms)
  Rendered links/_content_toggle.html.haml (0.2ms)
  CACHE (0.0ms)  SELECT `groups`.* FROM `groups` WHERE `groups`.`id` = 13 ORDER BY `groups`.`group_name` ASC LIMIT 1  [["id", 13]]
  Rendered links/_content_toggle.html.haml (0.8ms)
  Rendered links/_content_toggle.html.haml (0.2ms)
  CACHE (0.0ms)  SELECT `groups`.* FROM `groups` WHERE `groups`.`id` = 13 ORDER BY `groups`.`group_name` ASC LIMIT 1  [["id", 13]]
  Rendered links/_content_toggle.html.haml (1.0ms)
  Rendered links/_content_toggle.html.haml (0.2ms)
  Rendered links/_link_toggle.html.haml (0.4ms)
  Rendered links/_content_toggle.html.haml (0.3ms)
  CACHE (0.0ms)  SELECT `groups`.* FROM `groups` WHERE `groups`.`id` = 13 ORDER BY `groups`.`group_name` ASC LIMIT 1  [["id", 13]]
  Rendered links/_content_toggle.html.haml (0.9ms)
  Rendered links/_content_toggle.html.haml (0.2ms)

我的显示haml包含[partial]

  - @links.each_with_index do |link, index|
    - construct_hyperlink(link.url_address, link.alt_text, 'long')
    - long_url = @address_url
    - construct_hyperlink(link.url_address, link.alt_text, 'short')
    - short_url = @address_url
    - if link.group.group_name != (current_group ||= '')
      - display_group = current_group = link.group.group_name
      - row_bg_color_group = rotate_rows_color_group
    - else
      - display_group = ''
    %tr{class: "#{row_bg_color_group}"}
      %td
        = link_to display_group, link.group,{title: link.group.group_description}
      %td
        %span#mainLink
          = render :partial => 'content_toggle',
                   :locals => { :content => [long_url, short_url] }

注意:@conditions是一个复杂的硬编码sql子句

当我尝试

@links = 
Link.includes(:group).where(1=1).references(:group).order('groups.group_name, links.position')

在我得到的控制台中:

 => #<ActiveRecord::Relation [#<Link id: 524, url_address: "http://www.google.com", alt_text: "alt_text", group_id: 100000000, position: 323, created_at: "2014-09-11 02:25:20", updated_at: "2014-09-11 02:25:20", version_number: nil, content_date: nil, verified_date: "2014-09-10">, #<Link id: 525, url_address: "http://www.google.com", alt_text: "alt_text", group_id: 1000000000, position: 324, created_at: "2014-09-11 02:25:35", updated_at: "2014-09-11 02:25:35", version_number: nil, content_date: nil, verified_date: "2014-09-10">, #<Link id: 526, url_address: "http://www.google.com", alt_text: "alt_text", group_id: 999999, position: 325, created_at: "2014-09-11 02:26:33", updated_at: "2014-09-11 02:26:33", version_number: nil, content_date: nil, verified_date: "2014-09-10">, #<Link id: 527, url_address: "http://www.google.com", alt_text: "alt_text", group_id: 999998, position: 326, created_at: "2014-09-11 02:27:23", updated_at: "2014-09-11 02:27:37", version_number: nil, content_date: nil, verified_date: nil>, #<Link id: 532, url_address: "http://www.makeagoodpassword.com/", alt_text: "-", group_id: 42, position: 329, created_at: "2014-09-21 01:43:47", updated_at: "2014-09-28 21:53:54", version_number: "", content_date: "2014-09-28", verified_date: "2014-09-20">, #<Link id: 558, url_address: "http://lplp", alt_text: "", group_id: 42, position: 330, created_at: "2014-09-28 21:54:16", updated_at: "2014-09-28 21:54:16", version_number: "", content_date: "2014-09-28", verified_date: nil>, #<Link id: 147, url_address: "http://teethgrinder.co.uk/open-flash-chart/", alt_text: "Flash Charts IV", group_id: 20, position: 1, created_at: "2011-02-18 22:14:41", updated_at: "2014-08-10 00:58:10", version_number: "", content_date: "2012-06-11", verified_date: "2014-08-08">, #<Link id: 155, url_address: "http://www.fusioncharts.com/?BannerSource=IGPLWeb", alt_text: "Commercial Charting II", group_id: 20, position: 1, created_at: "2011-02-18 22:23:05", updated_at: "2014-07-12 23:10:52", version_number: "", content_date: nil, verified_date: "2014-07-12">, #<Link id: 165, url_address: "http://grafico.kilianvalkhof.com/", alt_text: "Javascript V", group_id: 20, position: 1, created_at: "2011-02-22 17:54:06", updated_at: "2014-07-12 17:41:26", version_number: "", content_date: nil, verified_date: "2014-07-12">, #<Link id: 148, url_address: "http://code.google.com/p/flot", alt_text: "Canvas Charts", group_id: 20, position: 1, created_at: "2011-02-18 22:16:07", updated_at: "2014-08-18 23:34:17", version_number: "", content_date: nil, verified_date: "2014-08-18">, ...]> 

但如果我真的尝试在我的应用程序中使用我的@condition我

NoMethodError in Links#index

Showing /home/durrantm/Dropnot/webs/rails_apps/linker/app/views/links/index.html.haml where line #31 raised:

undefined method `group_name' for nil:NilClass

Extracted source (around line #31):

- long_url = @address_url
- construct_hyperlink(link.url_address, link.alt_text, 'short')
- short_url = @address_url
- if link.group.group_name != (current_group ||= '') 

做解释告诉我:

Link.includes(:group).where('1=1').references(:group).order('groups.group_name, links.position').explain

 => EXPLAIN for: SELECT `links`.`id` AS t0_r0, `links`.`url_address` AS t0_r1, `links`.`alt_text` AS t0_r2, `links`.`group_id` AS t0_r3, `links`.`position` AS t0_r4, `links`.`created_at` AS t0_r5, `links`.`updated_at` AS t0_r6, `links`.`version_number` AS t0_r7, `links`.`content_date` AS t0_r8, `links`.`verified_date` AS t0_r9, `groups`.`id` AS t1_r0, `groups`.`group_name` AS t1_r1, `groups`.`created_at` AS t1_r2, `groups`.`updated_at` AS t1_r3, `groups`.`group_description` AS t1_r4 FROM `links` LEFT OUTER JOIN `groups` ON `groups`.`id` = `links`.`group_id` WHERE (1=1)  ORDER BY groups.group_name, links.position
+----+-------------+--------+--------+---------------+---------+---------+-----------------------------------+------+---------------------------------+
| id | select_type | table  | type   | possible_keys | key     | key_len | ref                               | rows | Extra                           |
+----+-------------+--------+--------+---------------+---------+---------+-----------------------------------+------+---------------------------------+
|  1 | SIMPLE      | links  | ALL    | NULL          | NULL    | NULL    | NULL                              |  380 | Using temporary; Using filesort |
|  1 | SIMPLE      | groups | eq_ref | PRIMARY       | PRIMARY | 4       | linker_development.links.group_id |    1 |                                 |
+----+-------------+--------+--------+---------------+---------+---------+-----------------------------------+------+---------------------------------+
2 rows in set (0.00 sec)

2.0.0-p481 :003 > 

架构:

mysql> describe links;
+----------------+--------------+------+-----+---------+----------------+
| Field          | Type         | Null | Key | Default | Extra          |
+----------------+--------------+------+-----+---------+----------------+
| id             | int(11)      | NO   | PRI | NULL    | auto_increment |
| url_address    | varchar(255) | NO   |     | NULL    |                |
| alt_text       | varchar(255) | YES  |     | NULL    |                |
| group_id       | int(11)      | YES  |     | NULL    |                |
| position       | int(11)      | YES  |     | NULL    |                |
| created_at     | datetime     | YES  |     | NULL    |                |
| updated_at     | datetime     | YES  |     | NULL    |                |
| version_number | varchar(255) | YES  |     | NULL    |                |
| content_date   | date         | YES  |     | NULL    |                |
| verified_date  | date         | YES  |     | NULL    |                |
+----------------+--------------+------+-----+---------+----------------+
10 rows in set (0.01 sec)

mysql> describe groups;
+-------------------+--------------+------+-----+---------+----------------+
| Field             | Type         | Null | Key | Default | Extra          |
+-------------------+--------------+------+-----+---------+----------------+
| id                | int(11)      | NO   | PRI | NULL    | auto_increment |
| group_name        | varchar(255) | NO   |     | NULL    |                |
| created_at        | datetime     | YES  |     | NULL    |                |
| updated_at        | datetime     | YES  |     | NULL    |                |
| group_description | varchar(255) | YES  |     | NULL    |                |
+-------------------+--------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

类:

class Group < ActiveRecord::Base
  has_many :links, :dependent => :destroy
  validates_presence_of :group_name
  validates_size_of :group_name, :maximum => 20
  default_scope -> { order(group_name: :asc)}
end

class Link < ActiveRecord::Base

  belongs_to :group
  validates_presence_of :url_address
  validates_presence_of :group_id
  validates_size_of :version_number, :maximum => 10 #, :allow_nil => true
  validates_uniqueness_of :url_address, scope: :group_id, message: 'already exists in that group'
  validate :url_address_not_just_http
  before_save :verify_url, if: :url_address_changed?
  acts_as_list
  ...# methods    
end

0 个答案:

没有答案