Topic: Noob Question Association

Hey there,

I just started R(oR) 2 days ago and still figuring out quite some stuff.
I kinda understand the scaffolding and associating between models.

However, as a next step i'm trying to make a category list and add some
posts under a pre-fab out of database category. I managed to save a post
with a cat_id manually via the rails console. However i can't seem to
read them out on my screen as such. I want Postname -> Catname instead
of Postname -> cat_id.

And with that, i'd like to get a dropdown/select input on my Post-adding
page with the categories and their ID's in it.

I tried out some tutorials(Like the

Re: Noob Question Association

Post some code (your corresponding models, views and controllers) to understand what exactly you want to do, - it would be better than some abstract discussions. smile

Re: Noob Question Association

I don't really have some real code at hand. Because it was the first thing i wanted to try with Rails after scaffolding.

What i have is: (Both just scaffolled and nothing more)
Post -> belongs to cats (So it has a cat_id and such)
Cat -> has many posts

Basicly, this works. When i create a post, it says cat_id = 1. Fine so far. But how do i get the categoryname corresponding to that 1? And how can i filter it by www.domain.com/cat/categoryname

What im trying to do is like JOINs in MySQL. (SELECT post.*, cat.* FROM posts,cat WHERE post.cat_id = 1 AND cat.cat_id = post.cat_id)

Last edited by shoopdawhoop (2012-02-27 06:10:13)

Re: Noob Question Association

post = Post.find(some_id) #finding a post
post_categories = post.categories #an array of the post categories
post_categories.each  do |category|
    puts "category name: #{category.categoryname}"
end

Adapt the code for your view.
See more detail on  Rails guides for associations

Re: Noob Question Association

Read this guide: http://guides.rubyonrails.org/getting_started.html

It's a HUGE help to new RoR users, especially since it sounds like you're making something like a blog.

Remember to edit your topic title and add "[SOLVED]" if your question has been answered!

Follow me!

Re: Noob Question Association

Thanks both, i actually managed to get it right pretty fast!

The next step however is to show my posts by category (www.domain.com/category/name). Can i do this from my category showview? And do i have access to my posts from there because of the inheritence?

Last edited by shoopdawhoop (2012-02-28 04:57:21)

Re: Noob Question Association

Yes. All your answers are in the guide I posted in my previous post. smile

Remember to edit your topic title and add "[SOLVED]" if your question has been answered!

Follow me!

Re: Noob Question Association

No sir, they are not. And if they are, i don't see them :-)

Re: Noob Question Association

Inside your categorys show.html.erb file:

<% @category.posts.each do |post|
  My post is <%= post.title %>!
<% end %>

smile

Remember to edit your topic title and add "[SOLVED]" if your question has been answered!

Follow me!