views:

41

answers:

2

Hi Everyone,

I have 2 models:

class Video < ActiveRecord::Base
belongs_to :categories, :foreign_key => "category", :class_name => "Category"
end

class Category < ActiveRecord::Base
has_many :videos
end

This is fine so far, in my videos controller for the index page I have:

def index
@videos = Video.all(:joins => :categories)

etc, etc
end

The above produces the following SQL Query: SELECT videos.* FROM videos INNER JOIN categories ON categories.id = videos.category

Which is fine up to a certain point, basically I need to get the category name (a field in that table) that way I don't have to do another call in the view to get category name based on the category id. Any ideas?

Thank you, and yes I am new to ruby, I tried reading the API but couldn't find much help there.

+1  A: 

If think the association in your Video class is set up incorrectly. It should be:

class Video < ActiveRecord::Base
  belongs_to :category
end

You can do @videos = Video.all(:include => :category). This will retrieve the video and associated category records using a single SQL statement.

Incidentally, your :class_name option on the belongs_to association is redundant because ActiveRecord will already have automatically inferred the class name from the association name. You should only use this option if you want to have an association name that is different from the underlying class e.g. authors/Person.

John Topley
When I do, the query changes to: SELECT * FROM `videos`No join or nothing, that's actually how I had it before and it never worked... :(
Jose Garcia
See my edited answer.
John Topley
Thanks John, the reason why I have the :class_name and renamed it to :categories, is because if I don't, when I edit I get the following error: ActiveRecord::AssociationTypeMismatch in VideosController#updateCategory(#2175068700) expected, got String(#2148246520)The only way I found to bypass this was by doing the above....I think I have too big of a mess on my hands lol.
Jose Garcia
Jose Garcia
It sounds like your associations are a bit messed up. Your `videos` table should have a `category_id` foreign key integer field if you set things up as per my answer. And it's impossible to answer your question about the edit and new screens without seeing your code! You should post it as a separate question.
John Topley
Thank you John!!!1 I changed the field to category_id and all my problems went away. I owe you a million bucks!!! lol
Jose Garcia
I'll look forward to receiving the money in the mail!
John Topley
A: 
join_condition = " ,categories where INNER JOIN categories ON categories.id=videos.category"

@videos = Video.all(:joins => join_condition)

try this

I don't think the join statement is correct: Mysql::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INNER JOIN categories ON categories.id=videos.category' at line 1: SELECT `videos`.* FROM `videos` ,categories where INNER JOIN categories ON categories.id=videos.category
Jose Garcia