# Newbie implementing one to many many to one relationship

**URL:** <https://www.ruby-forum.com/t/newbie-implementing-one-to-many-many-to-one-relationship/182927>\
**Category:** Rails\
**Created:** [February 4, 2010, 9:49pm UTC](https://www.ruby-forum.com/t/newbie-implementing-one-to-many-many-to-one-relationship/182927 "2010-02-04T21:49:56Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![mondegren](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/2bfe46/32.png) [@mondegren](https://www.ruby-forum.com/u/mondegren)\
**Post date:** [February 4, 2010, 9:49pm UTC](https://www.ruby-forum.com/t/newbie-implementing-one-to-many-many-to-one-relationship/182927/1 "2010-02-04T21:49:56Z")

</div>

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
