# Newbie: has\_many/belongs\_to trouble in my view

**URL:** <https://www.ruby-forum.com/t/newbie-has-many-belongs-to-trouble-in-my-view/142576>\
**Category:** Rails\
**Created:** [July 9, 2008, 11:34am UTC](https://www.ruby-forum.com/t/newbie-has-many-belongs-to-trouble-in-my-view/142576 "2008-07-09T11:34:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![scottnj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b9e5f3/32.png) [@scottnj](https://www.ruby-forum.com/u/scottnj)\
**Post date:** [July 9, 2008, 11:34am UTC](https://www.ruby-forum.com/t/newbie-has-many-belongs-to-trouble-in-my-view/142576/1 "2008-07-09T11:34:07Z")

</div>

i’m trying to learn some rails and have been doing great up to now.  
i have a has\_many/belongs\_to relationship. i got the new/edit forms  
working with a select box and i confirmed the urischeme\_id shows up in  
the websites table properly.

the problem is when i try to show the urischeme.scheme in my view. i  
keep getting errors. i tried pluralizing, caps, and everything i can  
think of!!! what am i doing wrong? i have been struggling with this  
for hours.

thanks,  
scott

// relevant models  
class Website \< ActiveRecord::Base  
belongs\_to :clubs  
belongs\_to :urischemes  
validates\_presence\_of :name, :address, :urischeme\_id  
end

class Urischeme \< ActiveRecord::Base  
has\_many :websites  
validates\_presence\_of :scheme  
end

// controller  
def show  
@club = Club.find(params[:id])

```
respond_to do |format|
  format.html # show.html.erb
  format.xml { render :xml => @club }
end

```

end

// my view code with comments added for this email  
\<% unless @club.websites.nil? %\>  
\<% @club.websites.each do |website| %\>  
\<%=h website.address -%\> // works fine  
\<%= debug(website) -%\> // no error, shows a proper  
urischeme\_id  
\<%= debug(website.urischeme) -%\> // undefined method  
`urischeme' for #<Website:0x477cd5c> <%= debug(website.urischeme.scheme) -%> // undefined method`urischeme’ for #Website:0x4743d90  
\<% end %\>  
\<% end %\>

---

<div class="post-metadata">

**Author:** ![scottnj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b9e5f3/32.png) [@scottnj](https://www.ruby-forum.com/u/scottnj)\
**Post date:** [July 9, 2008, 11:46am UTC](https://www.ruby-forum.com/t/newbie-has-many-belongs-to-trouble-in-my-view/142576/2 "2008-07-09T11:46:27Z")

</div>

You haven’t defined an association for urischeme.scheme (the validation  
doesn’t count as an association and may be wrong as well).

---

<div class="post-metadata">

**Author:** ![scottnj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b9e5f3/32.png) [@scottnj](https://www.ruby-forum.com/u/scottnj)\
**Post date:** [July 9, 2008, 5:15pm UTC](https://www.ruby-forum.com/t/newbie-has-many-belongs-to-trouble-in-my-view/142576/3 "2008-07-09T17:15:32Z")

</div>

scheme is a column in my urischeme table (see my migration below), why  
would I have to define a seperate association? the tables should be  
associated by  
Website = belongs\_to :urischemes  
Urischeme = has\_many :websites

as for the validations, I was just playing with them to learn how they  
work. they actually seem to work, plus if i delete them, i still get  
the same errors as above. so I don’t think they are causing my  
problem.

// the migration i used for creating the urischeme table  
class CreateUrischemes \< ActiveRecord::Migration  
def self.up  
create\_table :urischemes do |t|  
t.string :scheme

```
  t.timestamps
end
Urischeme.create(:scheme=>'http://')
Urischeme.create(:scheme=>'https://')

```

end

def self.down  
drop\_table :urischemes  
end  
end

---

<div class="post-metadata">

**Author:** ![scottnj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b9e5f3/32.png) [@scottnj](https://www.ruby-forum.com/u/scottnj)\
**Post date:** [July 9, 2008, 5:20pm UTC](https://www.ruby-forum.com/t/newbie-has-many-belongs-to-trouble-in-my-view/142576/4 "2008-07-09T17:20:49Z")

</div>

On 9 Jul 2008, at 06:12, skud wrote:

> thanks,  
> scott
> 
> // relevant models  
> class Website \< ActiveRecord::Base  
> belongs\_to :clubs  
> belongs\_to :urischemes

> ```
> <%= debug(website) -%> // no error, shows a proper
> 
> ```
> 
> urischeme\_id  
> \<%= debug(website.urischeme) -%\> // undefined method  
> `urischeme' for #<Website:0x477cd5c> <%= debug(website.urischeme.scheme) -%> // undefined method`urischeme’ for #Website:0x4743d90

Your association is belongs\_to :urischemes, so it will create a  
website.urischemes method, but you’re calling website.urischeme

Your association should be belongs\_to :urischeme (a belongs\_to is  
always singular)

Fred

---

<div class="post-metadata">

**Author:** ![scottnj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b9e5f3/32.png) [@scottnj](https://www.ruby-forum.com/u/scottnj)\
**Post date:** [July 9, 2008, 5:43pm UTC](https://www.ruby-forum.com/t/newbie-has-many-belongs-to-trouble-in-my-view/142576/5 "2008-07-09T17:43:45Z")

</div>

FINALLY SUCCESS!!!  
I knew it would be a simple fix.  
thanks
