# Model Question - First Rails Project

**URL:** <https://www.ruby-forum.com/t/model-question-first-rails-project/88734>\
**Category:** Rails\
**Created:** [March 13, 2007, 9:48pm UTC](https://www.ruby-forum.com/t/model-question-first-rails-project/88734 "2007-03-13T21:48:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![billbell52](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ce73a5/32.png) [@billbell52](https://www.ruby-forum.com/u/billbell52)\
**Post date:** [March 13, 2007, 9:48pm UTC](https://www.ruby-forum.com/t/model-question-first-rails-project/88734/1 "2007-03-13T21:48:48Z")

</div>

I am trying to do my first Rails project. I am having a problem  
understanding how to create the model. The project will track parts  
that are used in building an item. It will be hierarchical. For  
example a bicycle wheel is made from spokes, tire, rim and hub. A  
bicycle is made up of 2 wheels. Different bicycles will use different  
combinations of wheels.

I am using the book “Agile Web D. Using Rails” and the model  
I have so far is below. This case is not covered in the book. Are  
there any good examples of this out there. I am sure this is a common  
problem. Here are the tables.

```
create_table :parts do |t|
  t.column :name, :string, :limit => 255, :null =>

```

false  
t.column :version, :integer, :null =\> false  
t.column :create\_date, :timestamp, :null =\> false  
t.column :description, :text  
end

```
create_table :uses, :id => false do |t|
  t.column :part_id, :integer, :null => false
  t.column :part_parent_id, :integer, :null => false
  t.column :count, :integer, :null => false
end

```

I want to be able traverse up and down the hierarchy. Is this  
close to what I need?

class Part \< ActiveRecord::Base  
has\_many :used\_by, :class\_name =\> ‘Use’, :foreign\_key =\> “part\_id”  
has\_many :parents, :through =\> :used\_by, :class\_name =\>  
‘Part’, :foreign\_key =\> “part\_parent\_id”  
has\_many :uses, :class\_name =\> ‘Use’, :foreign\_key =\>  
‘part\_parent\_id’  
has\_many :parts, :through =\> :uses, :class\_name =\>  
‘Use’, :foreign\_key =\> “part\_id”  
end

class Use \< ActiveRecord::Base  
belongs\_to :parent, :class\_name =\> ‘Part’, :source  
=\> :part\_parent\_id  
belongs\_to :child, :class\_name =\> ‘Part’, :source =\> :part\_id  
end

---

<div class="post-metadata">

**Author:** ![billbell52](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ce73a5/32.png) [@billbell52](https://www.ruby-forum.com/u/billbell52)\
**Post date:** [March 13, 2007, 9:52pm UTC](https://www.ruby-forum.com/t/model-question-first-rails-project/88734/2 "2007-03-13T21:52:48Z")

</div>

billbell52 wrote:

> problem. Here are the tables.  
> t.column :part\_id, :integer, :null =\> false  
> ‘Part’, :foreign\_key =\> “part\_parent\_id”  
> end  
> Try this:

[http://wiki.rubyonrails.org/rails/pages/ActsAsTree](http://wiki.rubyonrails.org/rails/pages/ActsAsTree)

–  
Michael W.

---

<div class="post-metadata">

**Author:** ![billbell52](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ce73a5/32.png) [@billbell52](https://www.ruby-forum.com/u/billbell52)\
**Post date:** [March 15, 2007, 2:27am UTC](https://www.ruby-forum.com/t/model-question-first-rails-project/88734/3 "2007-03-15T02:27:48Z")

</div>

Bill,  
I’m new to this site and typically just read everyone else’s posts,  
but in this case I think I can help you with your bicycle metaphor.  
I come from a manufacturing background and we construct bicycles using  
a ‘Bill of Material’. This is simple a parent record for each ‘build’  
build that looks like this;  
BOM  
PartNo\_id  
Quantity  
(other fields if you want to get fancy)

The PartNo table obviously has  
id  
Descr  
Qty on hand, etc.

David
