【第9章】勤怠情報の表示&出勤・退勤できる画面を作ろう アウトプット
blongs_toについて
参照元モデルから参照先のモデルにアクセスすること。
例:AttendanceモデルからUserモデルに関連付ける場合、
userに対して1対1の関連性を示すコード :Attendance.rb
class Attendance < ApplicationRecord
belongs_to :user
end
belongs_to :userと記述することによって、userモデルに関連付けることが出来ます。
Attendanceモデル作成時に「user:references」を指定することにより、関連付けることが出来ます。
has_manyについて
先ほどのbelongs_toで、参照元→参照先の関係を作ることができました。
今度は、Userモデルに「has many]を記述し、attendancesと関連付けます。
attendanceモデルを全て関連付けるので複数形になります。
app/models/user.rb
class User < ApplicationRecord
has_many :attendances
.
.
.
end
has_many
を指定することで、1対多の関係性が出来ます。
テーブルの[colspan]と[rowspan]について
値を指定することにより、テーブルを結合して表示することが出来る。
属性 | 値 | 説明 |
---|---|---|
colspan="" |
結合するセルの数 | 水平方向の結合を指定 (初期値は 1 ) |
rowspan="" |
結合するセルの数 | 垂直方向の結合を指定 (初期値は 1 ) |
例えば、以下のコードがあり、" "で指定してある箇所があるとする。
<table class = "table-bordered table-striped table-condensed">
<thead>
<tr> <!--rowspan:縦結合、colspan:横結合-->
<th rowspan = "2">日付</th>
<th rowspan = "2">曜日</th>
<th colspan = "3">出社</th>
<th colspan = "3">退社</th>
<th rowspan = "2">在社時間</th>
<th rowspan = "2">備考</th>
</tr>
<tr>
<th>時</th>
<th>分</th>
<th></th>
<th>時</th>
<th>分</th>
<th></th>
</tr>
</thead>
実際にビューで確認すると、
こうなります。
それでは、さらばーゆ