# App\_Controller and partial issues with has\_many through

**URL:** <https://www.ruby-forum.com/t/app-controller-and-partial-issues-with-has-many-through/186229>\
**Category:** Rails\
**Created:** [April 2, 2010, 4:07pm UTC](https://www.ruby-forum.com/t/app-controller-and-partial-issues-with-has-many-through/186229 "2010-04-02T16:07:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![quangn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/e47774/32.png) [@quangn](https://www.ruby-forum.com/u/quangn)\
**Post date:** [April 2, 2010, 4:07pm UTC](https://www.ruby-forum.com/t/app-controller-and-partial-issues-with-has-many-through/186229/1 "2010-04-02T16:07:58Z")

</div>

I have a User model, a Post model, and an Interest model:  
User has\_many posts through interests  
User has\_many interests, dependent destroy  
Post has\_many users through interests  
has\_many interests, dependent destroy  
Post has\_many interests  
Interest belongs to Post  
Interest belongs to User

Application\_Controller is as follows:  
class ApplicationController \< ActionController::Base  
before\_filter :login\_from\_cookie  
before\_filter :find\_user\_interests  
helper :all # include all helpers, all the time  
session :session\_key =\> ‘\_blah\_session’

include AuthenticatedSystem

def find\_user\_interests  
@user\_interests = current\_user ? current\_user.interests : []  
true  
end

end

Application.html.erb has as follows:  
\<%= render :partial =\> “users/interests”, :object =\> @user\_interests %\>

\_interests.html.erb partial is as follows:

ul  
\<% unless current\_user.nil? then -%\>  
\<% @user\_interests.each do |interest| -%\>  
li\<%= interest.post.title %\>/li  
\<% end %\>  
\<% end -%\>  
/ul

Given all this when I at localhost:3000/posts/1 my partial shows up  
fine, but when in localhost:3000/posts I get an error undefined method  
‘title’ for nil:NilClass thus an error in the line li\<%=  
interest.post.title %\>/li shown above in the \_interests.html.erb  
partial.

What the heck would be the issue?

Also what is weird, if I take out the code in the partial that dislays  
the title, so to get around this error, I have another issue. I have a  
title in the partial that is set like “You have @user\_interests.length  
interests” and when I am on the posts page and say the posts page has 10  
posts my title reads like “You have 10 interests” so its counting all  
posts as an interest, even if the user hasn’t selected the post as an  
interest. And when I go to one specific Posts page, on that single Post  
page, the above referenced title reads “You have 1 interests” so its  
seems to be counting the number of posts on the specific pages (index or  
show) and totaling those up as a users interests. And if for one user I  
add two interests and view the single post page then the title will read  
“You have 3 interests”, thus counts the two interests I really am  
interested in plus the one for the Post page I am even though I have yet  
to click/select it as an interest.

TIA

---

<div class="post-metadata">

**Author:** ![quangn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/e47774/32.png) [@quangn](https://www.ruby-forum.com/u/quangn)\
**Post date:** [April 2, 2010, 5:01pm UTC](https://www.ruby-forum.com/t/app-controller-and-partial-issues-with-has-many-through/186229/2 "2010-04-02T17:01:05Z")

</div>

On 2 April 2010 15:07, Jason N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class ApplicationController \< ActionController::Base  
> end  
> \<% @user\_interests.each do |interest| -%\>
> 
> seems to be counting the number of posts on the specific pages (index or  
> show) and totaling those up as a users interests. And if for one user I  
> add two interests and view the single post page then the title will read  
> “You have 3 interests”, thus counts the two interests I really am  
> interested in plus the one for the Post page I am even though I have yet  
> to click/select it as an interest.

I would suggest installing ruby-debug, then you can break into the  
controller or the view and inspect the variables and work out what is  
going wrong.

See the rails guide on debugging at [http://guides.rubyonrails.org/](http://guides.rubyonrails.org/)

Colin

---

<div class="post-metadata">

**Author:** ![quangn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/e47774/32.png) [@quangn](https://www.ruby-forum.com/u/quangn)\
**Post date:** [April 3, 2010, 5:16am UTC](https://www.ruby-forum.com/t/app-controller-and-partial-issues-with-has-many-through/186229/3 "2010-04-03T05:16:39Z")

</div>

So I put this in my controller:  
logger.debug “Person attributes hash: #{@user\_interests.inspect}”

And got this:  
Person attributes hash: [#\<Interest id: 6, user\_id: 5, post\_id: 1,  
created\_at: “2010-04-01 23:52:53”, updated\_at: “2010-04-01 23:52:53”\>,  
#\<Interest id: 7, user\_id: 5, post\_id: 2, created\_at: “2010-04-01  
23:53:08”, updated\_at: “2010-04-01 23:53:08”\>]

Shouldn’t it be the interest.post info for each interest, not just the  
model attributes for the interest table?

---

<div class="post-metadata">

**Author:** ![quangn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/e47774/32.png) [@quangn](https://www.ruby-forum.com/u/quangn)\
**Post date:** [April 3, 2010, 4:26am UTC](https://www.ruby-forum.com/t/app-controller-and-partial-issues-with-has-many-through/186229/4 "2010-04-03T04:26:31Z")

</div>

When inspecting it I get [#,#,#,#] and my title reads “Your Interests  
(4)” and the print out is

# 

# 

# 

# 

What is # all about? Is that an object filled with objects?

Colin L. wrote:

> On 2 April 2010 15:07, Jason N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > class ApplicationController \< ActionController::Base  
> > ï¿½end  
> > ï¿½ ï¿½\<% @user\_interests.each do |interest| -%\>
> > 
> > seems to be counting the number of posts on the specific pages (index or  
> > show) and totaling those up as a users interests. ï¿½And if for one user I  
> > add two interests and view the single post page then the title will read  
> > “You have 3 interests”, thus counts the two interests I really am  
> > interested in plus the one for the Post page I am even though I have yet  
> > to click/select it as an interest.
> 
> I would suggest installing ruby-debug, then you can break into the  
> controller or the view and inspect the variables and work out what is  
> going wrong.
> 
> See the rails guide on debugging at [http://guides.rubyonrails.org/](http://guides.rubyonrails.org/)
> 
> Colin

---

<div class="post-metadata">

**Author:** ![quangn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/e47774/32.png) [@quangn](https://www.ruby-forum.com/u/quangn)\
**Post date:** [April 3, 2010, 9:42am UTC](https://www.ruby-forum.com/t/app-controller-and-partial-issues-with-has-many-through/186229/5 "2010-04-03T09:42:00Z")

</div>

On 3 April 2010 03:26, Jason N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> When inspecting it I get [#,#,#,#] Â and my title reads “Your Interests  
> (4)” and the print out is
> 
> # 
> 
> # 
> 
> # 
> 
> # 
> 
> What is # all about? Â Is that an object filled with objects?

Inspecting what? What happens if in the debugger you just type the  
variable name  
my\_variable\_name

Colin

---

<div class="post-metadata">

**Author:** ![quangn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/e47774/32.png) [@quangn](https://www.ruby-forum.com/u/quangn)\
**Post date:** [April 3, 2010, 9:39am UTC](https://www.ruby-forum.com/t/app-controller-and-partial-issues-with-has-many-through/186229/6 "2010-04-03T09:39:04Z")

</div>

On 3 April 2010 04:16, Jason N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Shouldn’t it be the interest.post info for each interest, not just the  
> model attributes for the interest table?

Inspect will only show you the array of Interest objects, which is  
what you have in @user\_interests, it does not dive down into the  
relationships.

Colin

---

<div class="post-metadata">

**Author:** ![quangn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/e47774/32.png) [@quangn](https://www.ruby-forum.com/u/quangn)\
**Post date:** [April 3, 2010, 3:13pm UTC](https://www.ruby-forum.com/t/app-controller-and-partial-issues-with-has-many-through/186229/7 "2010-04-03T15:13:33Z")

</div>

So I figured out the issue, but now I am wondering if setting this  
variable here isn’t a good idea, but rather doing it in a helper or  
somewhere else?

I am using AuthenticatedSystem and it has a function current\_user which  
of cource gets the current user. So in my Application controller where  
I am setting my @user\_interests variable it some how doesn’t have access  
to that function I guess. So as I created this function in the  
ApplicationController and it all works now.

def cur\_user  
User.find(session[:user\_id]) if session[:user\_id]  
end

So now my ApplicationController code looks like this:  
class ApplicationController \< ActionController::Base  
include AuthenticatedSystem

helper :all # include all helpers, all the time  
session :session\_key =\> ‘\_inchs\_session’

# See ActionController::RequestForgeryProtection for details

# Uncomment the :secret if you’re not using the cookie session store

protect\_from\_forgery # :secret =\> ‘eefa7eded747690e8efdb2044a729b4f’

before\_filter :login\_from\_cookie  
before\_filter :find\_user\_interests

protected  
def find\_user\_interests  
@user\_interests = cur\_user ? cur\_user.interests : []  
logger.debug “Person attributes hash: #{current\_user.inspect}”  
end

def cur\_user  
User.find(session[:user\_id]) if session[:user\_id]  
end

end
