Topic: Trying to display a relationship (newbie)

I have categories and links.

I set it up to the point where categories can be created. Links can be created and specify what category they belong to.

But if I try something like this =>

*links controller*

  def show
    @category = Category.find(params[:id])
    @link = Link.find(params[:id])
  end

and then this =>

*link show.rhtml*

<% for column in Link.content_columns %>
<p>
  <b><%= column.human_name %>:</b> <%=h @link.send(column.name) %>
</p>
<% end %>

<strong>Category:</strong> <%= @category.title %> <br />
<hr />
<%= link_to 'Edit', :action => 'edit', :id => @link %> |
<%= link_to 'Back', :action => 'list' %>


It doesn't work sad.

I get this error:

ActiveRecord::RecordNotFound in LinksController#show

Couldn't find Category with ID=4

Obviously I am doing something wrong with the relationships, so can someone point me in the right direction?

Thanks.

Last edited by DFischer (2006-12-23 19:10:30)

http://danielfischer.com - Personal Web-Technology-Blog, Los Angeles.

Re: Trying to display a relationship (newbie)

ID=4 is probably nil. What does your model look like? Is it something like:

class Category < ActiveRecord::Base
  has_many :links
end

class Link < ActiveRecord::Base
  belongs_to :category
end


If so, then forget the @link, blah blah. Go with this:

def show
  @category = Category.find(params[:id])  #presumes you have mysite.com/category/2 or something like that in the URL
end

Then, for links do this:

<p>Displaying links for <%= @category.title %></p>
<% for link in @category.links -%>
  <p>link: <%= link %></p>
<% end %>

I may have misunderstood your problem, but you can "test drive" how your model is working by firing up script/console and interacting with it direction. For example:

$ script/console
>> c = Category.find(:first)
----
>> c.links.inspect
----
>> c.links.each{|link| puts "*** #{link}"}

I put --- where there would be lots of output confirming your assumptions about your model's behavior.

Hope this helps.

Re: Trying to display a relationship (newbie)

Alright, that actually helped me make it work for displaying all links belonging to a category.

But what if I click show on a link, how would I make it display the category it belongs to?

http://danielfischer.com - Personal Web-Technology-Blog, Los Angeles.

Re: Trying to display a relationship (newbie)

I think you'll find the relationship cuts both ways. Try:

link.category

Re: Trying to display a relationship (newbie)

It seems you're trying to do too many things at once.  In this code:

   def show
     @category = Category.find(params[:id])
     @link = Link.find(params[:id])
   end

You're using the params[:id] to find both the category and the link.  It's almost never the case that the ids of your links and your categories line up correctly.  Most likely you want this:

   def show
     @category = Category.find(params[:id])
     @link = @category.link
   end

Or this:
   def show
     @link = Link.find(params[:id])
     @category = @link.category
   end

depending how you have your objects relating to each other.

Re: Trying to display a relationship (newbie)

Well since I'm displaying all links, and each link has a id (show passes the id of the current link)

It would be

def show
   @link = Link.find(params[:id])
end

Right? I hope so, otherwise I'd really not be understanding things.

So clicking show at this point just gives me all the columns for the links table in the current link. (name, url, etc).

But I want to pull in the relationship between the links and categories, and display what category the current link belongs to.

So if I try @category = @link.category it passes me errors:

undefined method `category' for #<Link:0x33b272c>

Any idea?

Sorry if this is a really newb problem. I just need help getting passed the noob stuff and then I get dwelve deeper into the beauty of rails smile (this is my learning process)

Last edited by DFischer (2006-12-24 20:31:44)

http://danielfischer.com - Personal Web-Technology-Blog, Los Angeles.

Re: Trying to display a relationship (newbie)

Have you set up the associations in the models as cwd mentioned?

class Category < ActiveRecord::Base
  has_many :links
end

class Link < ActiveRecord::Base
  belongs_to :category
end


This should generate a "category" method in the Link model so you won't get this error.

Railscasts - Free Ruby on Rails Screencasts

Re: Trying to display a relationship (newbie)

Oooh, I put belongs_to :categories instead of :category

Doh smile

http://danielfischer.com - Personal Web-Technology-Blog, Los Angeles.