Rails: get all assets belonging to a model -


i've got brand model has lot of assets:

class brand < activerecord::base   attr_accessible :name, :logo1, :logo2, :colour1, :colour2, :prices_attributes    has_attached_file :logo1   has_attached_file :logo2    has_many :users   has_many :welcome_messages   has_many :silos   has_many :articles, :through => :silos   has_many :libraries   has_many :covers, :through => :libraries   has_many :products   has_many :prices, :through => :products, :autosave => true, :dependent => :destroy    accepts_nested_attributes_for :prices  end 

is there quick way assets assigned each brand? or have brand.articles.each |article| ... each?

cheers!

if want eager load associations can following:

def self.all_associations   includes(:users, :articles, :prices, :products) #and other association end 

then can run brand.all_associations or brand.all_associations.find(1)

as jvnill wrote result in n database queries, in above example 5 database queries.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -