1

My project has a many-to-many relationship among the following classes:

class Project < ApplicationRecord
  has_many :project_software_processes, inverse_of: :project, :dependent => :delete_all
  has_many :software_processes, through: :project_software_processes

  accepts_nested_attributes_for :project_software_processes

  attr_accessor :project_attributes
end

..

class SoftwareProcess < ApplicationRecord
  has_many :project_software_processes
  has_many :projects, through: :project_software_processes
 end

--

class ProjectSoftwareProcess < ApplicationRecord
  belongs_to :software_process
  belongs_to :project
end

- -

I am using nested forms to create an instance of project so the user can pick as many software processes as they want:

<div class="field">
  <%= f.label :software_processes %>
  <%= f.fields_for :project_software_processes do |project_software_process|  %>
    <%= project_software_process.select(:software_processes_id, @processes.collect { |s| [s.name, s.id] } ) %>
    <%= project_software_process.link_to_remove "Remove this SoftwareProcess" %>
  <% end %>
  <p><%= f.link_to_add "Add a process", :project_software_processes %></p>
</div>

Problem is, when I click on submit I get the following validation message:

Project software processes software process must exist

It took me a bit to understand what the error actually means. I guess it is complaining about the existence of software process class when attempting to create an instance of project software process. However, there are instances of project software process, the user can even select them in the dropdown selects. Not sure if that's the issue though.

Here's some snippets from my project controller:

 def create
    @project = Project.new(project_params)
    @project.user = current_user

    respond_to do |format|
      if @project.save
        format.html { redirect_to @project, notice: 'Project was successfully created.' }
        format.json { render :show, status: :created, location: @project }
      else
        @processes = SoftwareProcess.all
        format.html { render :new }
        format.json { render json: @project.errors, status: :unprocessable_entity }
      end
    end
  end

--

def project_params
  params.require(:project).permit(:name, :user_id, project_software_processes_attributes: [:software_process_id, :_destroy])
end

--

Here's what is being sent in the params:

  Parameters: {"utf8"=>"✓", "authenticity_token"=>"[FILTERED]", "project"=>{"name"=>"aas", "project_software_processes_attributes"=>{"1484020859023"=>{"software_processes_id"=>"1", "_destroy"=>"false"}, "1484020859693"=>{"software_processes_id"=>"4", "_destroy"=>"false"}}}, "commit"=>"Create Project"}
5
  • Are you using Ryan Bate's nested_forms gem? If that's the case, I'd suggest not using his software anymore, he disappeared from Rails Casts and he's not giving support to his gems. Can you paste the actual params being sent to the controller? You can copy it from the log output. Commented Jan 10, 2017 at 4:22
  • @DarioBarrionuevo Yes, that's the one I'm using. Damn! Just realized it's very outdated. It works in another form though that I made for this same app. Anyways, suggestion of a better gem? Ps.: I edited the question and added the params output Commented Jan 10, 2017 at 4:32
  • Hmm.. I see "1484020859023"=>{"software_processes_id"=>"1" shouldn't it be "1484020859023"=>{"software_process_id"=>"1" (process in singular?) Try changing the select method to project_software_process.select(:software_process_id... in the view. Commented Jan 10, 2017 at 4:48
  • @DarioBarrionuevo Well noted! But now the form doesn't load anymore and I get undefined method `software_process_id' for #<ProjectSoftwareProcess:0xb04507d0> Did you mean? software_processes_id Commented Jan 10, 2017 at 5:26
  • @DarioBarrionuevo Just realized I made a mistake in my migation. I put: t.references :software_processes instead of t.references :software_process. I created a new migration to fix that so now everything works (I also had to do the change you suggested). Please, submit an answer so I can accept it. Commented Jan 10, 2017 at 5:43

1 Answer 1

1

The issue is on the name of the select method, should be software_process_id rather than software_processes_id. Try changing it to:

<%= project_software_process.select(:software_process_id, @processes.collect { |s| [s.name, s.id] } ) %>

Also, Ryan Bate's software is pretty outdated by now and nested_forms gem is not an exception. I've seen people recommending Cocoon as a suitable alternative. Take a look here

Cheers.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.