# Self Referential HABTM Checkbox Example or Tutorial Needed

**URL:** <https://www.ruby-forum.com/t/self-referential-habtm-checkbox-example-or-tutorial-needed/82750>\
**Category:** Rails\
**Created:** [January 20, 2007, 11:16am UTC](https://www.ruby-forum.com/t/self-referential-habtm-checkbox-example-or-tutorial-needed/82750 "2007-01-20T11:16:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eada6e/32.png) [@dan](https://www.ruby-forum.com/u/dan)\
**Post date:** [January 20, 2007, 11:16am UTC](https://www.ruby-forum.com/t/self-referential-habtm-checkbox-example-or-tutorial-needed/82750/1 "2007-01-20T11:16:10Z")

</div>

My checkboxes currently show up either checked or not checked based on  
manually entered data using this bit so far…

\<% Project.find(:all).each do |project| %\>  
\<input type=“checkbox” id="\<%= project.id %\>" name=“project\_ids[]”  
value="\<%= project.id %\>"\<% if @project.cross\_reference.include? project  
%\> checked=“checked”\<% end %\>\> \<%= project.name %\>

I have projects that relate to other projects in a cross\_reference table  
with rows for project\_id and cross\_reference\_id.

My manually enter values can be retrieved but I can’t figure out how to  
write the values to the cross\_reference table from a web form.

Any reference or guidance appeciated.

Thanks,  
DAN

---

<div class="post-metadata">

**Author:** ![dan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eada6e/32.png) [@dan](https://www.ruby-forum.com/u/dan)\
**Post date:** [January 20, 2007, 2:29pm UTC](https://www.ruby-forum.com/t/self-referential-habtm-checkbox-example-or-tutorial-needed/82750/2 "2007-01-20T14:29:29Z")

</div>

Just a suggestion: use has\_many :through instead. habtm is kinda  
deprecated, at least partly.

---

<div class="post-metadata">

**Author:** ![dan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eada6e/32.png) [@dan](https://www.ruby-forum.com/u/dan)\
**Post date:** [January 21, 2007, 1:39pm UTC](https://www.ruby-forum.com/t/self-referential-habtm-checkbox-example-or-tutorial-needed/82750/3 "2007-01-21T13:39:52Z")

</div>

I dunno if this helps you, but in HowTo:  
[http://wiki.rubyonrails.com/rails/pages/Howtos](http://wiki.rubyonrails.com/rails/pages/Howtos)  
you will find a document about many to many relations.

Besides, in  
[http://www.fallenrogue.com/article/view/143-I-am-what-I-am-or-self-referential-joins-in-Rails](http://www.fallenrogue.com/article/view/143-I-am-what-I-am-or-self-referential-joins-in-Rails)  
you will find an example on self-referential joins…

---

<div class="post-metadata">

**Author:** ![dan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eada6e/32.png) [@dan](https://www.ruby-forum.com/u/dan)\
**Post date:** [January 20, 2007, 10:17pm UTC](https://www.ruby-forum.com/t/self-referential-habtm-checkbox-example-or-tutorial-needed/82750/4 "2007-01-20T22:17:10Z")

</div>

Thanks for the direction.

I’ve added a cross\_reference model now and in my  
models/cross\_reference.rb is

has\_many :projects  
belongs\_to :project

and my models/project.rb contains

has\_many :cross\_references  
has\_many :projects, :through =\> :cross\_references

I’m able to show manually entered data, such as discriminaing which  
checkboxes for related projects should be checked - same as before, but  
now using the code in my project form partial

\<% Project.find(:all).each do |project| %\>  
\<input type=“checkbox” id="\<%= project.id %\>" name=“project\_ids[]”  
value="\<%= project.id %\>"\<% if @project.projects.include? project %\>  
checked=“checked”\<% end %\>\> \<%= project.name %\>  
  
\<% end %\>

I’m still stuck on trying to save (via my project edit or update  
methods) to my cross\_references table though.

I believe I need some statements in my project\_controller.rb new,  
create, and update methods such as:

def project\_new  
@project = Project.new  
@cross\_references = CrossReference.find(:all)  
end

def project\_create  
@project = Project.new(params[:project])  
@project.cross\_references =  
CrossReference.find(@params[:cross\_reference\_ids]) if  
@params[:cross\_reference\_ids]  
end

def project\_edit  
@project = Project.find(params[:id])  
@cross\_references = CrossReference.find(:all)  
end

def project\_update  
@project = Project.find(params[:id])  
@project.cross\_references =  
CrossReference.find(@params[:cross\_reference\_ids]) if  
@params[:cross\_reference\_ids]  
end

I hope I’m poviding enough information. It would be great to understand  
how the create these kind of relationships.

DAN
