Top > Ruby on Railsあれこれ > テーブル結合をしてみましょう

  0
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
<html>
<head>
  <title>Tutorial: Employee List</title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <%= stylesheet_link_tag 'ror' %>
</head>
<body>
<h2>Tutorial: Employee List</h2>
 
<table border="1">
  <tr style="background-color:pink">
    <th>Id</th><th>Name</th><th>JobType</th><th>Salary</th><th>Department</th><th>Address</th><th>編集</th>
  </tr>
  <% for e in @employees %>
  <tr style="background-color:<%= cycle('white', 'yellow') %>">
    <td align="right"><%=h e.id %></td>
    <td><%=h e.name %></td>
    <td><%=h e.job_type %></td>
    <td align="right"><%= number_with_delimiter(e.salary, delimiter=",") %></td>
    <td><%=h e.department.name %></td>
    <td><%=h e.address.name %></td>
    <td><%= link_to "編集", {:action => 'edit', :id => e.id} %></td>
  </tr>
  <% end %>
</table>
</body>
</html>
  0
  1
  belongs_to :department, :foreign_key => 'department_id'
  belongs_to :address, :foreign_key => 'address_id'
  0
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
class Employee < ActiveRecord::Base
  
  belongs_to :department, :foreign_key => 'department_id'
  belongs_to :address, :foreign_key => 'address_id'
  
  validates_presence_of :name, :salary, :message => "を入力して下さい"
  
  #バリデーションエラーメッセージ内のプロパティ名を日本語にマッピングします。
  class << self
    HUMANIZED_ATTRIBUTE_KEY_NAMES = {
      "name" => "氏名",
      "salary" => "給与額"
    }
  
    def human_attribute_name(attribute_key_name)
      HUMANIZED_ATTRIBUTE_KEY_NAMES[attribute_key_name] || super
    end
  end
  
end
  0
  1
class Address < ActiveRecord::Base
end
  0
  1
  2
  3
  4
  5
  6
  #一覧画面表示
  def index2
    @departments = Department.find(:all)
    #@employees = Employee.find(:all, :include=> [:department, :address])
    @employees = Employee.find(:all)
    render :action => 'index2.html.erb'
  end
  0
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
  Employee Columns (0.000000)   SHOW FIELDS FROM `employees`
  Department Columns (0.000000)   SHOW FIELDS FROM `departments`
  Department Load (0.000000)   SELECT * FROM `departments` WHERE (`departments`.`id` = 3) 
  Address Columns (0.000000)   SHOW FIELDS FROM `addresses`
  Address Load (0.000000)   SELECT * FROM `addresses` WHERE (`addresses`.`id` = 1) 
  CACHE (0.000000)   SELECT * FROM `departments` WHERE (`departments`.`id` = 3) 
  Address Load (0.000000)   SELECT * FROM `addresses` WHERE (`addresses`.`id` = 2) 
  Department Load (0.000000)   SELECT * FROM `departments` WHERE (`departments`.`id` = 2) 
  Address Load (0.000000)   SELECT * FROM `addresses` WHERE (`addresses`.`id` = 3) 
  CACHE (0.000000)   SELECT * FROM `departments` WHERE (`departments`.`id` = 3) 
  Address Load (0.000000)   SELECT * FROM `addresses` WHERE (`addresses`.`id` = 4) 
  CACHE (0.000000)   SELECT * FROM `departments` WHERE (`departments`.`id` = 3) 
  Address Load (0.000000)   SELECT * FROM `addresses` WHERE (`addresses`.`id` = 5) 
  Department Load (0.000000)   SELECT * FROM `departments` WHERE (`departments`.`id` = 1) 
  Address Load (0.000000)   SELECT * FROM `addresses` WHERE (`addresses`.`id` = 6) 
  CACHE (0.000000)   SELECT * FROM `departments` WHERE (`departments`.`id` = 2) 
  Address Load (0.000000)   SELECT * FROM `addresses` WHERE (`addresses`.`id` = 7) 
  CACHE (0.000000)   SELECT * FROM `departments` WHERE (`departments`.`id` = 1) 
  Address Load (0.000000)   SELECT * FROM `addresses` WHERE (`addresses`.`id` = 8) 
  CACHE (0.000000)   SELECT * FROM `departments` WHERE (`departments`.`id` = 3) 
  Address Load (0.000000)   SELECT * FROM `addresses` WHERE (`addresses`.`id` = 9) 
  CACHE (0.000000)   SELECT * FROM `departments` WHERE (`departments`.`id` = 2) 
  Address Load (0.000000)   SELECT * FROM `addresses` WHERE (`addresses`.`id` = 10) 
  CACHE (0.000000)   SELECT * FROM `departments` WHERE (`departments`.`id` = 3) 
  Address Load (0.000000)   SELECT * FROM `addresses` WHERE (`addresses`.`id` = 11) 
  CACHE (0.000000)   SELECT * FROM `departments` WHERE (`departments`.`id` = 2) 
  Address Load (0.000000)   SELECT * FROM `addresses` WHERE (`addresses`.`id` = 12) 
  CACHE (0.000000)   SELECT * FROM `departments` WHERE (`departments`.`id` = 1) 
  Address Load (0.000000)   SELECT * FROM `addresses` WHERE (`addresses`.`id` = 13) 
  CACHE (0.000000)   SELECT * FROM `departments` WHERE (`departments`.`id` = 2) 
  Address Load (0.000000)   SELECT * FROM `addresses` WHERE (`addresses`.`id` = 14) 
  0
  1
    @employees = Employee.find(:all, :include=> [:department, :address])
    #@employees = Employee.find(:all)
  0
  1
  2
  3
  4
  Employee Columns (0.000000)   SHOW FIELDS FROM `employees`
  Department Columns (0.000000)   SHOW FIELDS FROM `departments`
  Department Load (0.000000)   SELECT * FROM `departments` WHERE (`departments`.`id` IN (1,2,3)) 
  Address Columns (0.000000)   SHOW FIELDS FROM `addresses`
  Address Load (0.000000)   SELECT * FROM `addresses` WHERE (`addresses`.`id` IN (11,6,12,7,13,8,14,9,1,2,3,10,4,5)) 
Rendering employees/index2.html.erb

添付ファイル: filedb_tuto_join_1.jpg 286件 [詳細]

リロード   新規 編集 凍結解除 差分 添付 複製 名前変更   ホーム 一覧 単語検索 最終更新 バックアップ リンク元   ヘルプ   最終更新のRSS
Last-modified: 2012-08-08 (水) 23:25:39 (2923d)