blasterpal wrote:
> class Article < ActiveRecord::Base
> ...
> ARTS_CATEGORY = Category.find(:first, :conditions => ['name LIKE ?',
> 'arts%']).id
>   OPINE_CATEGORY = Category.find(:first, :conditions => ['name
> LIKE ?', 'opine%']).id
>   DISTRACTIONS_CATEGORY = Category.find(:first, :conditions => ['name
> LIKE ?', 'distra%']).id
>   NEWS_POLITICS_CATEGORY = Category.find(:first, :conditions => ['name
> LIKE ?', 'news%']).id
> 
> ...
> 
> So in my controllers I use a method like this (the actual method is a
> shared method for all "types" longer, this is for readability:
> ...
>   def arts_archive
>     Article.find_by_category_id(Article::ARTS_CATEGORY)
>   end
> ...
> 


I think a better way to do this is

class Category < ActiveRecord::Base
  has_many :articles #or has_one :article
  def self.arts
    # You could probably also do this with a named scope,
    # but I don't remember the syntax off the top of my
    # for selecting just one object.
    Category.find(:first, :conditions => ['name LIKE ?', 'arts%']).id
  end
end

and then in your controller:
  Category.arts.articles



-- 
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to