ruby on rails - How should I use the alias_method_chain for the build method? -


i using ruby on rails 3.2.13 , use alias_method_chain :build, :option_name statement since getting strange error. is, ...

... in controller file have:

class articles::commentscontroller < applicationcontroller   def create     @articles_comment = @article.comments.build(params[:comment])     ...   end end 

... in model file have:

class articles::comment < activerecord::base   def self.build_with_option_name     ...   end    alias_method_chain :build, :option_name end 

when run create controller action following error in log:

actioncontroller::routingerror (undefined method `build' class `articles::comment'):   app/models/articles/comment.rb:5:in `<class:comment>' 

how should use alias_method_chain build method? or, maybe better, should proceed in way reach make (for example, should overwrite build method in articles::comment model instead of using alias_method_chain)?


note i: don't know if helps, build method refers association (@article.comments). more, not state build method in articles::comment model because should "added" / "attached" class ruby on rails framework (i think made through meta-programming).

note ii: same error occurs when considering new method instead of build; is, when using alias_method_chain :new, :option_name.

as said, build method defined on association proxy. can use association extensions, in model can pass block has_many call, treated extension given association_proxy:

class article < activerecord::base   ...   has_many :comments     alias_method_chain :build, :option_name   end 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

.htaccess - Matching full URL in RewriteCond -