PROBLEM I have two models, Blog and BlogComment. When a blog is initially created, it has no comments. Upon creation, the title and body are automatically added to the ferret index and directly searchable. However, when a comment is added to a blog, that comment does not get added to the index and is therefore not ferretable. The desired behavior is that when a comment is added to a blog, that the comment be ferretable. CURRENT SETUP Blog (id, title, body, user_id) BlogComment (id, blog_id, comment) class Blog < ActiveRecord::Base has_many :blog_comments, :dependent => :destroy acts_as_ferret :additional_fields => [:blog_comments] def blog_comments self.blog_comments.collect {|comment| comment.body } end end class BlogComment < ActiveRecord::Base belongs_to :blog end CONTROLLER [..] if @blog.blog_comments << comment do_something else do_something end Can someone recommend a good approach to automatically updating the index when a comment is added? -- Posted via http://www.ruby-forum.com/.
On Thu, Feb 01, 2007 at 02:42:38AM +0100, Mark wrote:> PROBLEM > I have two models, Blog and BlogComment. When a blog is initially > created, it has no comments. Upon creation, the title and body are > automatically added to the ferret index and directly searchable. > However, when a comment is added to a blog, that comment does not get > added to the index and is therefore not ferretable. The desired behavior > is that when a comment is added to a blog, that the comment be > ferretable. > > CURRENT SETUP > Blog (id, title, body, user_id) > BlogComment (id, blog_id, comment) > > class Blog < ActiveRecord::Base > has_many :blog_comments, :dependent => :destroy > acts_as_ferret :additional_fields => [:blog_comments] > > def blog_comments > self.blog_comments.collect {|comment| comment.body } > end > > end > > class BlogComment < ActiveRecord::Base > belongs_to :blog > end > > CONTROLLER > [..] > if @blog.blog_comments << comment > do_somethingadding @blog.ferret_update here should do the trick.> else > do_something > end >Jens -- webit! Gesellschaft f?r neue Medien mbH www.webit.de Dipl.-Wirtschaftsingenieur Jens Kr?mer kraemer at webit.de Schnorrstra?e 76 Tel +49 351 46766 0 D-01069 Dresden Fax +49 351 46766 66
Jens Kraemer wrote:>> if @blog.blog_comments << comment >> do_something > > adding > @blog.ferret_update > here should do the trick. > >> else >> do_something >> endJens, that''s one option I had considered. What do you think about creating an onsave event in all the models that use acts_as_ferret like so.. after_save :update_ferret_index def update_ferret_index self.blog.ferret_update if self.blog end -- Posted via http://www.ruby-forum.com/.
On Thu, Feb 01, 2007 at 02:54:41PM +0100, Mark wrote:> Jens Kraemer wrote: > > >> if @blog.blog_comments << comment > >> do_something > > > > adding > > @blog.ferret_update > > here should do the trick. > > > >> else > >> do_something > >> end > > Jens, that''s one option I had considered. What do you think about > creating an onsave event in all the models that use acts_as_ferret like > so.. > > after_save :update_ferret_index > > def update_ferret_index > self.blog.ferret_update if self.blog > endyeah, this should work, too. Even prettier from an architectural point of view :-) Jens -- webit! Gesellschaft f?r neue Medien mbH www.webit.de Dipl.-Wirtschaftsingenieur Jens Kr?mer kraemer at webit.de Schnorrstra?e 76 Tel +49 351 46766 0 D-01069 Dresden Fax +49 351 46766 66
Jens Kraemer wrote:> On Thu, Feb 01, 2007 at 02:54:41PM +0100, Mark wrote: >> >> do_something >> end > yeah, this should work, too. Even prettier from an architectural point > of view :-) > > Jens > > > -- > webit! Gesellschaft f?r neue Medien mbH www.webit.de > Dipl.-Wirtschaftsingenieur Jens Kr?mer kraemer at webit.de > Schnorrstra?e 76 Tel +49 351 46766 0 > D-01069 Dresden Fax +49 351 46766 66Hi. i have the same problem, but my_instance.ferret_update doesn''t index associated model fields on the console. Doesn''t ferret_update get called after_save anyway? -- Posted via http://www.ruby-forum.com/.