Home > Mobile >  how can update the value of null column in rails
how can update the value of null column in rails

Time:09-16

I am trying to create multiple bug against one project, but it show the error in edit_task_params

  param is missing or the value is empty: project

when i create the project the bug_id is null i just want to add bug ids against the project but i don't know how can i fix this issue .

project model

class Project < ApplicationRecord
   has_many :bugs
end

bug model class Bug < ApplicationRecord belongs_to :project end

bug_controller

class BugController < ApplicationController
    
    def new
      @bug = Bug.new
    end

    def create

      parameters = bug_params.merge({ user_id: current_user.id })   

      @bug = Bug.new(parameters)
      parameter = edit_task_params.merge({ bug_id: @bug.id })
      @project = Project.find(params[:project_id])
      @project = Project.update( parameter)
      respond_to do |format|
        if @bug.save
          @project = Project.find(params[:project_id])
          @project.update(edit_task_params)
          format.html { redirect_to new_bug_path, flash: { success: "bug added"} }
        else
          format.html { render :new }
        end
      end
   end

 private
 def bug_params
     params.require(:bug).permit(:title, :description, :screen_shot, :typeOf, :status_is, :deadline, :user_id, :project_id )
 end

 def edit_task_params
     params.require(:project).permit(:bug_id)
 end

end

CodePudding user response:

First, we need to look at whether your approach to solving the problem is good enough or not?

Why does the Project model need to store the bug_id?

To answer this, we need to think of how one_to_many relation works.

Let's say

  • One Project has many Bugs
  • Many Bugs Belongs to a Single Project

Two Approaches comes to mind

  1. We should store the bugs ids to Project table| model
  2. We should put project_id in bugs table | model

how it will make our Project table look like

Wrong Project Table

As you can see we have to store multiple values of bugs_ids As a single project can have many bugs and this structure is not recommended as it violates the first normalization. So it is ignored

Now we move towards the second way of doing this

Bugs Sample Table

Porjects Table So we will be using this approach

Now come back to rails and your solution

1- You don't need bug_id in Project model so remove it. 2- You don't need to update Project while creating bug remove that part also. 3- You should not be performing update in create method which I assume you trying to do.

So after performing the necessary corrections you code would be looking like this.

parameters = bug_params.merge({ user_id: current_user.id })   

bug = Bug.new(parameters)
if bug.save
  format.html { redirect_to new_bug_path, flash: { success: "bug added"} }
else
  format.html { render :new }
 end

You can access your bugs using projects in this way

bugs = Project.find(project_id).bugs

You need to create a separate method for updating and editing.

  • Related