ruby - Calling Class method via Rails ActiveRecord Association -


i have 2 classes as:

class post < activerecord::base   has_many :comments end  class comment < activerecord::base   belongs_to :post    # class method   # analysis on comments of given post     def self.do_sentiment_analysis      post_id = self.new.post_id  # there better way post_id?       # real code follows    end  end   # class method called on post object this: post.find(1).comments.do_sentiment_analysis 

the question whether there better way know id of association object (post) on class method called. 1 way (which used above) is: post_id = self.new.post_id. bet there cleaner way don't have create object post_id.

to answer question directly,

comment.scope_attributes 

will return hash of attributes current scope on comment set. can test effect association has on

i'm not sure i'd use though - seems little odd have class method can invoked on scopes of particular form.


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -