Topic: I'm getting into a steamy three-way relationship tonight

Finally I have some time to start flexing my Rails muscle. Whew.

Anyway, I have a bit of an issue. As a really simplified example, imagine I have three models/tables:

articles (id,name,subcategory_id)
subcategories (id,name,category_id)
categories (id,name)

So basically when I write an article, it's associated with a subcategory, and from that subcategory I derive the parent category. There's no real link between article and top category at this point, and that's posing a problem. I could put additional category information in the articles table, but that seems like it would add a lot of unwanted repetition. The specific problem right now I have is that I'd love to Article.find_by_topic_id, but at this point there's no relationship set up for that. I'm still wrapping my head around how Rails does relationships and what might be the proper way to do all of this. Any ideas or directions I could head in? Thanks a bunch.

Good-Tutorials. Now with tutorials for Ruby and for Rails.

Re: I'm getting into a steamy three-way relationship tonight

If you do has_many :through like this:

class Category < ActiveRecord::Base
  has_many :subcategories
  has_many :articles, :through => :subcategories
end

You can fetch the articles of a given category like this:

@category.articles

Railscasts - Free Ruby on Rails Screencasts

Re: I'm getting into a steamy three-way relationship tonight

totally thought this post was spam, and then was looking forward to a witty reply from ryanb, but alas smile

Re: I'm getting into a steamy three-way relationship tonight

Me too LOL. Too bad I'm not very witty. The title was definitely asking for it.

Railscasts - Free Ruby on Rails Screencasts

Re: I'm getting into a steamy three-way relationship tonight

have a look at http://api.rubyonrails.org/classes/Acti … ml#M000566
acts_as_tree

can sub-categories and parent-categories be of the same type ?
isn't a sub-category a category with a parent / a category a sub-category without a parent ?


pascal

Re: I'm getting into a steamy three-way relationship tonight

pascal's got a good idea, use acts_as_tree for all your categories and link parents to children using a parent_id column. This way you only need two tables: articles and categories. Yeah it's not as adventurous and fun as the three-way, but it's a much more stable and well-tested relationship model that works for the majority of the population, and nobody will be left with hurt feelings (you're not the only one who can do innuendo dammit wink).

PS: i'm so glad I actually read threads with titles like these instead of automatically moving them tongue

vinnie - rails forum admin

Re: I'm getting into a steamy three-way relationship tonight

We all know why you really read them vin wink

Re: I'm getting into a steamy three-way relationship tonight

thabenksta wrote:

We all know why you really read them vin wink

Hey being admin is a tough job, at least let me enjoy the perks big_smile

vinnie - rails forum admin

Re: I'm getting into a steamy three-way relationship tonight

I'm sure no one *really* cares too much after these few days, but the three-way fell through and I decided to stay monogamous, although I now do have the option to go with the parent of the child that I currently have the relationship with.

Okay, terribleness aside... thanks a bunch. Everything seems to be working well, and this is a lot more clear to me. I nearly got the has_many :through aspect figured out, but I missed a step until you posted, Ryan. But I got that working and then retooled it a bit to use acts_as_tree. So it's like learning two things at the same time!

It's a little weird and frustrating to learn all of these concepts in Ruby and Rails, but at the same time it can be really fun. It'll be an enlightening next few weeks, I think.

And really, Vinnie, you thought I of all people would spam? The "lo0ol" bell should have rung in your head after you browsed the topic title and you should have flown into entertainment mode. I really have been gone for too long, it seems.

Good-Tutorials. Now with tutorials for Ruby and for Rails.

Re: I'm getting into a steamy three-way relationship tonight

Actually, on this same note- is there an easy way to find all the articles for any given category? So if I have the ID of the parent category, I want to grab all the articles for that parent category. It's easy to get the articles in a given subcategory since that's listed in the database, but I'm running into the problem of getting the link through subcategories into parent categories.

Good-Tutorials. Now with tutorials for Ruby and for Rails.

Re: I'm getting into a steamy three-way relationship tonight

Are you using acts_as_tree? Unfortunately this makes it more difficult because SQL isn't that good at representing tree relationships. You may want to try acts_as_nested_set or just create a custom find_by_sql command.

Railscasts - Free Ruby on Rails Screencasts

Re: I'm getting into a steamy three-way relationship tonight

Yeah, I'm currently using acts_as_tree. Seems like I might be shifting around once more, lol. acts_as_nested_set seems to be a little bit of an overdose for what I'm trying to do. I'll try playing around with creating my own solution, but I guess if that doesn't work I'll head back to maybe what I had originally. Though if someone has any other clever ways to do it, by all means, let me know. I've already picked up a lot about these different methods just from exploring them all like this- learning a new framework and language is fun.

Good-Tutorials. Now with tutorials for Ruby and for Rails.

Re: I'm getting into a steamy three-way relationship tonight

Just to make sure I understand you correctly, you want to find all articles in a given category including all its subcategories? acts_as_tree might actually work with the following code.

Article.find('category_id IN (?)', @category.self_and_all_children)

# in category.rb
def self_and_all_children
  [self] + children.collect(&:self_and_all_children)
end


Untested.

Railscasts - Free Ruby on Rails Screencasts