activerecord - Rails generate records for secondary model on create -
i making sort of checklist section site. have model called commission contain data commissioning task. need when new commission entry created need create series of 30 commission tasks link it. sort of checklist of predefined values person go down through , check. best way this?
here models , controller:
commission.rb
class commission < applicationrecord has_many :comtasks belongs_to :project belongs_to :user accepts_nested_attributes_for :comtasks, allow_destroy: true end
comtask.rb
class comtask < applicationrecord belongs_to :commission belongs_to :user end
commissions_controller.rb
class commissionscontroller < applicationcontroller before_action :set_commission, only: [:show, :edit, :update, :destroy] # /commissions # /commissions.json def index @commissions = commission.all end # /commissions/1 # /commissions/1.json def show end # /commissions/new def new @commission = commission.new end # /commissions/1/edit def edit end # post /commissions # post /commissions.json def create @commission = commission.new(commission_params) respond_to |format| if @commission.save format.html { redirect_to @commission, notice: 'commission created.' } format.json { render :show, status: :created, location: @commission } else format.html { render :new } format.json { render json: @commission.errors, status: :unprocessable_entity } end end end # patch/put /commissions/1 # patch/put /commissions/1.json def update respond_to |format| if @commission.update(commission_params) format.html { redirect_to @commission, notice: 'commission updated.' } format.json { render :show, status: :ok, location: @commission } else format.html { render :edit } format.json { render json: @commission.errors, status: :unprocessable_entity } end end end # delete /commissions/1 # delete /commissions/1.json def destroy @commission.destroy respond_to |format| format.html { redirect_to commissions_url, notice: 'commission destroyed.' } format.json { head :no_content } end end private # use callbacks share common setup or constraints between actions. def set_commission @commission = commission.find(params[:id]) end # never trust parameters scary internet, allow white list through. def commission_params params.require(:commission).permit(:project_id, :user_id, :description, :objectname, :location, comtasks_attributes: [:id, :content, :notes]) end end
any thoughts or suggestions appreciated.
below idea,
def create @commission = commission.create!(commission_params) # use create not new generate @commission.id value # comtask records can use id value reference create_comtasks_job # comtask create put in other method respond_to |format| if @commission.save format.html { redirect_to @commission, notice: 'commission created.' } format.json { render :show, status: :created, location: @commission } else format.html { render :new } format.json { render json: @commission.errors, status: :unprocessable_entity } end end end def create_comtasks_job # loop 30 tasks / or manual follow @commission.comtasks.build(content: 'content1',notes:'notes1') @commission.comtasks.build(content: 'content2',notes:'notes2') end
additional code model
make sure model has relation sample below
for model
class commission < activerecord::base has_many :comtasks end class comtask < activerecord::base belongs_to :commission end
Comments
Post a Comment