# Help With associations

**URL:** <https://www.ruby-forum.com/t/help-with-associations/111834>\
**Category:** Rails\
**Created:** [August 11, 2007, 11:00pm UTC](https://www.ruby-forum.com/t/help-with-associations/111834 "2007-08-11T23:00:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![charlie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d6d6ee/32.png) [@charlie](https://www.ruby-forum.com/u/charlie)\
**Post date:** [August 11, 2007, 11:00pm UTC](https://www.ruby-forum.com/t/help-with-associations/111834/1 "2007-08-11T23:00:30Z")

</div>

I am having some problems manipulating associations. For example, I have  
an opinion model that belongs to both a user model and a blog model.  
When listing a users blogs I want to also list that users opinion of the  
blog. So in my view/blogs/list, I have some code like this:

\<% @op = blog.opinions(:user\_id =\> current\_user.id)%\>  
 \<%= h(@op.rating)%\>

rating is the field name in my opinions table. This however returns me  
the following error message:

undefined method `rating’ for Opinion:Class

Since I do have a rating field in my opinions table, I’m not exactly  
sure what is wrong with this. Thanks,  
Charlie

---

<div class="post-metadata">

**Author:** ![charlie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d6d6ee/32.png) [@charlie](https://www.ruby-forum.com/u/charlie)\
**Post date:** [August 11, 2007, 11:47pm UTC](https://www.ruby-forum.com/t/help-with-associations/111834/2 "2007-08-11T23:47:40Z")

</div>

did you put  
attr\_accessor :rating  
at the start of your opinion class ? This makes the rating getter and  
setter available for other classes  
HTH,

Elise

On Aug 11, 11:00 pm, Charlie W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![charlie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d6d6ee/32.png) [@charlie](https://www.ruby-forum.com/u/charlie)\
**Post date:** [August 12, 2007, 12:09am UTC](https://www.ruby-forum.com/t/help-with-associations/111834/3 "2007-08-12T00:09:11Z")

</div>

Hmm,  
I just put that at the beginning of my opinion model and it still  
didn’t work.  
Also, I had never heard about attr\_accessor before, ill have to look  
into it.

removed\_email\_address@domain.invalid wrote:

> did you put  
> attr\_accessor :rating  
> at the start of your opinion class ? This makes the rating getter and  
> setter available for other classes  
> HTH,
> 
> Elise
> 
> On Aug 11, 11:00 pm, Charlie W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![charlie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d6d6ee/32.png) [@charlie](https://www.ruby-forum.com/u/charlie)\
**Post date:** [August 12, 2007, 8:52pm UTC](https://www.ruby-forum.com/t/help-with-associations/111834/4 "2007-08-12T20:52:11Z")

</div>

Thanks,  
Could someone provide me with a simple explanation on when to use  
instance variables vs. when not to?

---

<div class="post-metadata">

**Author:** ![charlie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d6d6ee/32.png) [@charlie](https://www.ruby-forum.com/u/charlie)\
**Post date:** [August 12, 2007, 4:58pm UTC](https://www.ruby-forum.com/t/help-with-associations/111834/5 "2007-08-12T16:58:56Z")

</div>

For starters … the @op variable does not need to have the “@” symbol  
inside the view. That makes it an “instance” …

2nd … you’re not getting back an “Opinion” object where you do  
“blog.opinions( … )” you’re getting back an “array of opinion”  
objects.

You need to do something similar to this …

\<% opinions = blog.opinions( :user\_id =\> current\_user.id ) %\>

\<% for opinion in opinions %\>

 \<%= h( opinion.rating ) %\> \<% end %\>

Hope that helps, man.

Good luck.

On Aug 11, 4:00 pm, Charlie W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![charlie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d6d6ee/32.png) [@charlie](https://www.ruby-forum.com/u/charlie)\
**Post date:** [August 13, 2007, 3:19am UTC](https://www.ruby-forum.com/t/help-with-associations/111834/6 "2007-08-13T03:19:52Z")

</div>

Charlie W. wrote:

> Thanks,  
> Could someone provide me with a simple explanation on when to use  
> instance variables vs. when not to?

HI Charlie,

I had written this in response to a question here. It doesn’t  
specifically address instance variables but gives the general feeling  
about what they do.  
[http://groups.google.com/group/rubyonrails-talk/browse\_thread/thread/2ae42439587d32b9/4e71bbb39243f06e?l#4e71bbb39243f06e](http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/2ae42439587d32b9/4e71bbb39243f06e?l#4e71bbb39243f06e)

Hopefully this will give you some idea. If it doesn’t, I hope that  
someone can give you a better explanation 🙂

Cheers,  
Mohit.  
8/13/2007 | 9:18 AM.
