LoginSignup
1
1

More than 5 years have passed since last update.

社員全体のスケジュール表を作りたい

Last updated at Posted at 2016-10-14

子モデルから、親モデルと孫モデルを取得してbuildする方法が知りたい。

各社員がログイン後、今週の予定と曜日・時間を入力→保存できるシステムを作りたいと考えています。

社員(memberモデル)を親にもつ予定(planモデル)、日時(date)があります。
※dateはplanから見たら更に子モデル

member plan date
id id id
name member_id member_id
- description plan_id
- - day_type
- - time

質問

①現状、予定を入力するとdateモデル「member_id」だけ入力されずに保存されます。
planから親モデルであるmemberと、子モデルであるdateをbuildする方法を教えて頂けませんでしょうか。

②このモデル設計で、カレンダーに「dateの:day_typeとtime」を元に「memberのname」と「planのdescription」を該当する日時に入力させることはできますでしょうか?

マイグレーション

20160803113715_create.member.rb
class CreateMember < ActiveRecord::Migration
  def change
    create_table :Members do |t|
      t.text         :name
      t.timestamps
    end
  end
end
20160803113715_create.plan.rb
class CreatePlan < ActiveRecord::Migration
  def change
    create_table :Plans do |t|
      t.integer   :member_id
      t.text         :description
      t.timestamps
    end
  end
end
20160803113715_create.date.rb
class CreateDate < ActiveRecord::Migration
  def change
    create_table :Dates do |t|
      t.integer   :member_id
      t.integer   :day_type
      t.date      :time
      t.timestamps
    end
  end
end

モデル

member.rb
class Member < ActiveRecord::Base
  belongs_to :company
  has_many :dates
  has_many :plans, through: :date
  accepts_nested_attributes_for :plans
plan.rb
class Plan < ActiveRecord::Base
  belongs_to :member
  has_many :dates, dependent: :destroy
  accepts_nested_attributes_for :dates
date.rb
class Date < ActiveRecord::Base
  belongs_to :member
  belongs_to :plan

ルーティング

routes.rb
Rails.application.routes.draw do
resources :mambers do
    resources :plans
end

コントローラ

plans_controller.rb
class PlansController < ApplicationController
 def new
    @member = Member.find(params[:member_id])
    @plan   = @member.plans.build
    @plan.dates.build
    respond_with(@plan)
  end

  def edit
  end

  def create
    @member = Member.find(params[:member_id])
    @plan   = @member.plans.build(plan_params)
    @plan.member_id = @member.id

    if @plan.save
      redirect_to :root, notice: '予定を入力しました'
    else
      render :new
    end
  end

private
  def plan_params
    params.require(:plan).permit(:description,
    dates_attributes: [:member_id, :plan_id, :day_type, :time])
  end

ビュー(入力画面のみ)

plans/new.html.erb
<h1><%= @member.name %>さんの予定入力</h1>
<%= render 'form' %>
plans/_form.html.erb
<%= simple_form_for([@member, @plan]) do |f| %>
  <div class="form-inputs">
    <%= f.input :description %>
  </div>

  <%= f.fields_for :dates do |f| %>
    <%= render partial: 'dates', locals: {f: f} %>
  <% end %> 

  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>

_dates_html.erb
<div class="form_time_selects">
    <%= f.label :day_type %></li>
    <ul>
      <li>毎日: <%= f.radio_button 'day_type', '0', {} %></li>
      <li>月: <%= f.radio_button 'day_type', '1', {} %></li>
      <li>火: <%= f.radio_button 'day_type', '2', {} %></li>
      <li>水: <%= f.radio_button 'day_type', '3', {} %></li>
      <li>木: <%= f.radio_button 'day_type', '4', {} %></li>
      <li>金: <%= f.radio_button 'day_type', '5', {} %></li>
      <li>土: <%= f.radio_button 'day_type', '6', {} %></li>
      <li>日: <%= f.radio_button 'day_type', '7', {} %></li>
    </ul>
    <ol>
      <li><%= f.time_select :time, minute_step: 10, include_blank: true %></li>
    </ol>
</div>
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1