# Has\_many/belongs\_to question

**URL:** <https://www.ruby-forum.com/t/has-many-belongs-to-question/137133>\
**Category:** Rails\
**Created:** [May 9, 2008, 8:35am UTC](https://www.ruby-forum.com/t/has-many-belongs-to-question/137133 "2008-05-09T08:35:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hrmm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/edb3f5/32.png) [@hrmm](https://www.ruby-forum.com/u/hrmm)\
**Post date:** [May 9, 2008, 8:35am UTC](https://www.ruby-forum.com/t/has-many-belongs-to-question/137133/1 "2008-05-09T08:35:56Z")

</div>

Is it possible to automatically add data to the belongs\_to end of a  
hm/bt relationship?

For clarity, here are my models:

class Page; has\_many :links; end  
class Link; belongs\_to :page; end

Is there anyway that I can do something like this:

Page.create( :name =\> ‘foo’, :links =\> [{:name =\> “bleh”, :url =\>  
“[www.bleh.com](http://www.bleh.com)”}, {:name =\> “blah”, :url =\> “[www.blah.com](http://www.blah.com)”}] )

Right now I am doing something like this in my controller:

def create  
@page = Page.new(params[:page])  
links = params[:links]  
if @page.save  
links.each{|e| @page.links.create(:name =\> e[:name], :url =\>  
e[:url])  
end  
end

But I feel there must be a more intuitive way of doing this.  
Basically, I want to add my links collection (from a form at /pages/new)  
to the page hash and create them automatically. Is this possible?

---

<div class="post-metadata">

**Author:** ![hrmm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/edb3f5/32.png) [@hrmm](https://www.ruby-forum.com/u/hrmm)\
**Post date:** [May 9, 2008, 6:57pm UTC](https://www.ruby-forum.com/t/has-many-belongs-to-question/137133/2 "2008-05-09T18:57:57Z")

</div>

Hi,

I think you can say:

if @page.save  
@page.links.create(params[:links])  
end

I think that’s a happier way to say it 🙂

Julian.

Learn Ruby on Rails! Check out the FREE VIDS (for a limited time)  
VIDEO #4 parts a and b now available!  
[http://sensei.zenunit.com/](http://sensei.zenunit.com/)

---

<div class="post-metadata">

**Author:** ![hrmm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/edb3f5/32.png) [@hrmm](https://www.ruby-forum.com/u/hrmm)\
**Post date:** [May 10, 2008, 7:58pm UTC](https://www.ruby-forum.com/t/has-many-belongs-to-question/137133/3 "2008-05-10T19:58:19Z")

</div>

Julian L. wrote:

> Hi,
> 
> I think you can say:
> 
> if @page.save  
> @page.links.create(params[:links])  
> end
> 
> I think that’s a happier way to say it 🙂
> 
> Julian.
> 
> Learn Ruby on Rails! Check out the FREE VIDS (for a limited time)  
> VIDEO #4 parts a and b now available!  
> [http://sensei.zenunit.com/](http://sensei.zenunit.com/)

I have a feeling that;

@page = Page.new(params[:page])  
@page.links = params[:links]  
@page.save

will do all the saves in a nice transaction,  
returning false, and having created no objects if any of the validation  
fails.

Haven’t got anything set up to try it though.
