# Making a new join in a habtm relationship

**URL:** <https://www.ruby-forum.com/t/making-a-new-join-in-a-habtm-relationship/110453>\
**Category:** Rails\
**Created:** [July 26, 2007, 2:04pm UTC](https://www.ruby-forum.com/t/making-a-new-join-in-a-habtm-relationship/110453 "2007-07-26T14:04:50Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [July 26, 2007, 2:04pm UTC](https://www.ruby-forum.com/t/making-a-new-join-in-a-habtm-relationship/110453/1 "2007-07-26T14:04:50Z")

</div>

I have a class (Article) that has a habtm relationship with ‘links’,  
where links are also Articles. I’ve got a join table called  
articles\_links, and have set up the relationship so that it understands  
what’s going on (i think):

1. class Article \< ActiveRecord::Base
2. has\_and\_belongs\_to\_many :links,
3. 

```
:class_name => "Article",

```

4. 

```
:association_foreign_key => "link_id",

```

5. 

```
:join_table => "articles_links"

```

When i make a new link, using a form, the article is created fine but i  
can’t work out how to create the join. I’m sure this is simple but i  
can’t find out how. My ‘create link’ method looks like this:

1. def create\_link
2. 

```
@link = Article.new(params[:article])

```

3. 

```
#set some attributes of the new object

```

4. 

```
if @link.title == ""

```

5. 

```
  @link.title =

```

(Hpricot(open(@link.url))/“title”).first.inner\_html  
6. end  
7. @link.added\_at = DateTime.now.to\_s  
8. @link.user\_id = session[:user].id  
9. @link.points = 0  
10. #then save it and return to the original article page  
11. if @link.save  
12. flash[:notice] = ‘Link was successfully created.’  
13. redirect\_to :action =\> ‘show’, :id =\> params[:article].id  
14. else  
15. flash[:warning] = ‘Could not create link’  
16. render :action =\> ‘new\_link’  
17. end  
18. end

Can anyone explain to me how to set up the join properly?
