# Frustrated with rails has\_many problem

**URL:** <https://www.ruby-forum.com/t/frustrated-with-rails-has-many-problem/77193>\
**Category:** Ruby\
**Created:** [November 25, 2006, 12:37am UTC](https://www.ruby-forum.com/t/frustrated-with-rails-has-many-problem/77193 "2006-11-25T00:37:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![shawn\_bright](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@shawn\_bright](https://www.ruby-forum.com/u/shawn_bright)\
**Post date:** [November 25, 2006, 12:37am UTC](https://www.ruby-forum.com/t/frustrated-with-rails-has-many-problem/77193/1 "2006-11-25T00:37:41Z")

</div>

hello there all,

i am writing a website in rails, i love it, but i am banging my head  
here .

ok, i have a table called sites ( means customer sites )  
each site has\_many :site\_notes  
so, site\_note belongs\_to :site

i used a scaffold to generate the site notes files. site notes work  
kinda  
like comments to a blog.  
a customer has sites and for each site, i want him to be able to post  
notes  
about what what his machines are doing.

so, my link to the sites controller looks like this.  
\<%= link\_to “site notes”, :action =\> “list”,  
:id =\> @site,  
:controller =\> ‘site\_notes’ %\>

and in the site\_notes\_controller.rb i have this  
def list  
@site = Site.find(params[:id])  
@site\_notes = SiteNote.find(:all, :conditions =\> [“site\_id”,  
@site.id])  
@site\_note\_pages, @site\_notes = paginate :site\_notes, :per\_page =\>  
10  
end

everything seems to work, it lists the right notes, i can edit and  
delete  
them etc…  
but when i try to add a new one, it tells me that it cannot find a Site  
without an id.  
so, i guess i need to know how to put something either in the model or  
controller, (or view, whatever works)  
that will automatically add site\_id = @site.id when i create a new note

thanks  
sk

---

<div class="post-metadata">

**Author:** ![shawn\_bright](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@shawn\_bright](https://www.ruby-forum.com/u/shawn_bright)\
**Post date:** [November 25, 2006, 12:39am UTC](https://www.ruby-forum.com/t/frustrated-with-rails-has-many-problem/77193/2 "2006-11-25T00:39:41Z")

</div>

You need to do that by hand, sorry.

---

<div class="post-metadata">

**Author:** ![shawn\_bright](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@shawn\_bright](https://www.ruby-forum.com/u/shawn_bright)\
**Post date:** [November 25, 2006, 12:45am UTC](https://www.ruby-forum.com/t/frustrated-with-rails-has-many-problem/77193/3 "2006-11-25T00:45:42Z")

</div>

ok, how ?

---

<div class="post-metadata">

**Author:** ![shawn\_bright](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@shawn\_bright](https://www.ruby-forum.com/u/shawn_bright)\
**Post date:** [November 25, 2006, 1:37am UTC](https://www.ruby-forum.com/t/frustrated-with-rails-has-many-problem/77193/4 "2006-11-25T01:37:52Z")

</div>

On 11/24/06, shawn bright [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > like comments to a blog.  
> > > def list  
> > > them etc…
> > 
> > –  
> > Chris C.  
> > [concentrationstudios.com](http://concentrationstudios.com)  
> > [brynmawrcs.com](http://brynmawrcs.com)

so, in my situation, would i need to keep passing the Site.id (or, for  
the  
site\_notes table, site\_id) as a variable from action to action in the  
controller?

thanks  
sk

---

<div class="post-metadata">

**Author:** ![shawn\_bright](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@shawn\_bright](https://www.ruby-forum.com/u/shawn_bright)\
**Post date:** [November 25, 2006, 2:47am UTC](https://www.ruby-forum.com/t/frustrated-with-rails-has-many-problem/77193/5 "2006-11-25T02:47:57Z")

</div>

you can do this:

\<%= link\_to “site notes”, :action =\> “list”,  
:id =\> @site.id, # here will using @site.id  
:controller =\> ‘site\_notes’ %\>

and in the site\_notes\_controller.rb i have this

def list  
@site = Site.find(params[:id])  
@site\_notes = @site.site\_notes # if you use has\_many association you  
can  
just write this  
@site\_note\_pages, @site\_notes = paginate :site\_notes, :per\_page =\> 10  
end

2006/11/25, shawn bright [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![shawn\_bright](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@shawn\_bright](https://www.ruby-forum.com/u/shawn_bright)\
**Post date:** [November 25, 2006, 3:24am UTC](https://www.ruby-forum.com/t/frustrated-with-rails-has-many-problem/77193/6 "2006-11-25T03:24:01Z")

</div>

On 11/24/06, ao tianlong [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> @site = Site.find(params[:id])
> 
> > > > > i am writing a website in rails, i love it, but i am banging my  
> > > > > kinda
> > > > > 
> > > > > end  
> > > > > controller, (or view, whatever works)  
> > > > > Chris C.
> > 
> > thanks  
> > sk

thanks, that helped a lot, i am able to move around the controller  
passing

> @site to each next action, and putting it in the form as a hidden field.  
> Thanks for the tip there

sk
