I’ve written a model for “Category”. The requirements here are that each category can fall into one category “type”. I’m learning rails at the same time as doing this project, and managed to get the above working with the following class method (where_category_type);
class Category < ActiveRecord::Base
#associations
belongs_to :category_type
has_and_belongs_to_many :recipes
def self.where_category_type category_type
Category.find(:all, :include => :category_type, :conditions => { :category_types => {:name => category_type }})
end
end
All works etc. but I am very keen to make sure I’m doing things “the rails way”, so I was wondering if I’m missing some syntactic sugar somewhere that would make this a little more readable / less verbose?
Then instead of defining *where_category_type* Category’s class static method, you can call just: