I am attempting to tie a Project to Projectparts to Parts and I believe the relationship should be one-to-many, then many-to-one. My goal is to build the Parts table so that this table can grow and the Parts descriptions can be re-used in different Projects (ultimately tie this in to an autocomplete plug-in so the user doesn't have to re- type a part every time, but I'm not there yet). I had the application working through to the Project - Projectparts relationship, I am now trying to add the description component from the Part table.
I think I've tracked down my problem to the _projectpart partial of code that I've implemented for this. Here's the partial:
<div class="fields"> <%= f.text_field :qty, :size => 3 %>
<% f.fields_for :parts do |builder| %> # <======I think problem is here. <%= builder.text_field :description, :size => 50 %> <% end %>
<%= remove_child_link "Delete", f %> </div>
As the code is now, the 'New' view will render, but when I save the data from the UI, I get an unknown attribute error in my 'Create':
ActiveRecord::UnknownAttributeError in ProjectsController#create
unknown attribute: parts
However, when I change the :parts reference to :part, the New screen will not render & I get a nil object error:
NoMethodError in Projects#new
Showing app/views/projects/_projectpart.html.erb where line #10 raised:
You have a nil object when you didn't expect it! You might have expected an instance of ActiveRecord::Base. The error occurred while evaluating nil.new_record?
So I'm thinking I'm not referencing this correctly. I've tried using @project.projectpart.description, @project.projectpart & other variations, but not sure if the model is set up correctly in the first place (the description is the 'one' at the end of the one-to-many / many-to-one relationship).
Any thoughts? Thanks in advance.
For reference: