App_Controller and partial issues with has_many through

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

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/

Colin

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

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?

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?

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

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

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