# Has\_many\_and\_belongs\_to\_"one"? beginner question, go easy o

**URL:** <https://www.ruby-forum.com/t/has-many-and-belongs-to-one-beginner-question-go-easy-o/109969>\
**Category:** Rails\
**Created:** [July 20, 2007, 7:30pm UTC](https://www.ruby-forum.com/t/has-many-and-belongs-to-one-beginner-question-go-easy-o/109969 "2007-07-20T19:30:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Astorian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ad7895/32.png) [@Astorian](https://www.ruby-forum.com/u/Astorian)\
**Post date:** [July 20, 2007, 7:30pm UTC](https://www.ruby-forum.com/t/has-many-and-belongs-to-one-beginner-question-go-easy-o/109969/1 "2007-07-20T19:30:51Z")

</div>

Greetings. I am new to RoR, and to programming in general, so I  
apologize if the solution is trivial.

My goal is to keep track of the 5 most common “Tag\_Relationships” in a  
tagging scheme. This would let me ask such things as “what tag  
accompanies the tag ‘computer’ most often, second most often” etc… I  
figured this might be easier than storing an Array with the last\_five  
per Agile Web D. edition 2. I chose to use the acts\_las\_list  
feature.

my question involves two models

Tag  
…  
has\_many :tag\_relationships, :order =\> :position  
validates\_uniqueness\_of :name

Tag\_Relationship  
belongs\_to :tag, :foreign\_key =\> ‘related\_tag\_id’  
acts\_as\_list :scope =\> :tag  
validates\_uniqueness\_of :tag\_id, :scope =\> :related\_tag\_id

Questions. Something looks fishy to me in the belongs\_to for  
Tag\_Relationships. I had to do this to get it to work, but really, the  
tag\_relationship belongs to the original tag, not the referred tag.  
What is the correct way to use has\_many / belongs\_to in this case? I  
would like to be able to do the following

t = Tag.find(1)  
trs = t.tag\_relationships

and then iterate over the collection and get the name for each  
referred tag. My tag\_relationships table is as follows

create\_table “tag\_relationships”, :force =\> true do |t|  
t.column “tag\_id”, :integer  
t.column “related\_tag\_id”, :integer  
t.column “position”, :string #ranking  
for this particular tag  
t.column “relationships\_count”, :integer, :default =\> 1 #number  
of times this relationship has been recorded  
end

Thanks in advance

John

---

<div class="post-metadata">

**Author:** ![Astorian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ad7895/32.png) [@Astorian](https://www.ruby-forum.com/u/Astorian)\
**Post date:** [July 20, 2007, 10:09pm UTC](https://www.ruby-forum.com/t/has-many-and-belongs-to-one-beginner-question-go-easy-o/109969/2 "2007-07-20T22:09:43Z")

</div>

Astorian wrote:

> Greetings. I am new to RoR, and to programming in general, so I  
> apologize if the solution is trivial.  
> Thanks in advance
> 
> John

Well, I did not use act as, but I know you can do this…

t = Tag.find(1)  
trs = t.tag\_relationships

…when you use has\_many and belongs\_to 🙂
