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: ======================== Models: class Project < ActiveRecord::Base validates_presence_of :name has_many :projectparts, :dependent => :destroy accepts_nested_attributes_for :projectparts, :reject_if => lambda { |a| a.values.all?(&:blank?) }, :allow_destroy => true def projectpart_attributes=(projectpart_attributes) projectpart_attributes.each do |attributes| projectpart.build(attributes) end end end class Projectpart < ActiveRecord::Base belongs_to :project has_one :part accepts_nested_attributes_for :parts, :reject_if => lambda { |a| a.values.all?(&:blank?) } end class Part < ActiveRecord::Base has_and_belongs_to_many :projectparts end My db schema - the foreign key for accessing the Part description is in the projectparts table since there should be many of these records and I don't want to unnecessarily increase the number of records in the Parts table, but perhaps this is causing part of the problem? create_table "projects", :force => true do |t| t.string "name" t.string "description" end create_table "projectparts", :force => true do |t| t.integer "project_id" t.integer "part_id" t.datetime "created_at" t.datetime "updated_at" t.integer "qty" end create_table "parts", :force => true do |t| t.string "description" t.datetime "created_at" t.datetime "updated_at" end -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-t...@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.