Hi, I got a mailer that uses dynamic stylesheets, so when creating an email I''m doing Style.find_by_name("newsletter"). The problem is that it''s fetching the same Style record for each email it creates and when sending newsletters there can be a lot of them. I could simply fetch this record in controller and just pass it as a parameter for deliver_email method, but it doesn''t seem right for me - I''d like to keep as much as possible inside mailer class. I can''t also simply put STYLE = Style.find_by_name("newsletter") into Mailer class, because if admin changes the style, the mailer would still use cached object. Is there a *simple* way to do it? This Style object can only be updated - it can''t be destroyed. I''m using Rails 2.1.1. --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
If you dont want to get into memcache or other complex solutions you can do something simple like this: class StylesCache include Singleton def self.by_name(name) self.instance.by_name(name) end def by_name(name) return @active_style if @active_style && @active_style.name =name @active_style = Style.find_by_name(name) return @active_style end end style = StylesCache.by_name("newsletter") On Oct 22, 4:36 pm, szimek <szi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > I got a mailer that uses dynamic stylesheets, so when creating an > email I''m doing Style.find_by_name("newsletter"). > > The problem is that it''s fetching the same Style record for each email > it creates and when sending newsletters there can be a lot of them. I > could simply fetch this record in controller and just pass it as a > parameter for deliver_email method, but it doesn''t seem right for me - > I''d like to keep as much as possible inside mailer class. > > I can''t also simply put STYLE = Style.find_by_name("newsletter") into > Mailer class, because if admin changes the style, the mailer would > still use cached object. > > Is there a *simple* way to do it? This Style object can only be > updated - it can''t be destroyed. I''m using Rails 2.1.1.--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks for the answer, but I needed a way to clear the "cache" after the cached object was updated. Finally made it like this: class NewsletterMailer def self.style @@style ||= fetch_style end def self.reload_style @@style = fetch_style end end and I''m calling NewsletterMailer.reload_style in Style#after_update callback. On Oct 23, 2:05 am, Andrew Bloom <akbl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> If you dont want to get into memcache or other complex solutions you > can do something simple like this: > > class StylesCache > include Singleton > > def self.by_name(name) > self.instance.by_name(name) > end > > def by_name(name) > return @active_style if @active_style && @active_style.name => name > > @active_style = Style.find_by_name(name) > return @active_style > end > end > > style = StylesCache.by_name("newsletter") > > On Oct 22, 4:36 pm, szimek <szi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Hi, > > > I got a mailer that uses dynamic stylesheets, so when creating an > > email I''m doing Style.find_by_name("newsletter"). > > > The problem is that it''s fetching the same Style record for each email > > it creates and when sending newsletters there can be a lot of them. I > > could simply fetch this record in controller and just pass it as a > > parameter for deliver_email method, but it doesn''t seem right for me - > > I''d like to keep as much as possible inside mailer class. > > > I can''t also simply put STYLE = Style.find_by_name("newsletter") into > > Mailer class, because if admin changes the style, the mailer would > > still use cached object. > > > Is there a *simple* way to do it? This Style object can only be > > updated - it can''t be destroyed. I''m using Rails 2.1.1.--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---