ruby on rails - Validation methods in plugins -


i using ruby on rails 3.0.7 , trying implement act_as_article plugin application. run validation methods "acting article class" inside plugin (note: plugin requires create database table columns in order work - 1 of these represented title attribute).

in ror application have code:

# vendor/plugins/article/lib/acts_as_article.rb module article   extend activesupport::concern    included     validates :title,  # validation method       :presence => true   end    module classmethods     def acts_as_article       send :include, instancemethods     end   end    module instancemethods     ...   end end  activerecord::base.send :include, article   # app/models/review.rb class review   acts_as_article    ... end 

using above code plugin works. if add record association in review class this:

class review   acts_as_article    has_many :comments # adding association    ... end 

and in reviewscontroller add following, well:

def create   ...    @article.comments.build(   # code line 89     :user_id => @user.id   )    if @article.save     ...   end end 

i error

nomethoderror (undefined method `title' #<comments:0x00000103abfb90>):   app/controllers/articles_controller.rb:89:in `create' 

probably happens because validation run review "associated" classes\models , comment class doesn't have title attribute. think because if inside plugin code comment out validation method this

module article   ...    included     # validates :title,  # validation     #   :presence => true   end    ... end 

i don't errors anymore.

so, how can solve issue?

note: not expert on creating plugin (this first time), ask implicitly if i'm doing job plugin implementation...

you including validates_presence_of :title in activerecord::base, , every active record model picking up. instead, should do:

# vendor/plugins/article/lib/acts_as_article.rb module article   extend activesupport::concern     module classmethods     def acts_as_article       validates :title,  # add validation method here         :presence => true       send :include, instancemethods     end   end    module instancemethods     ...   end end 

so include validation on activerecord models expect validation go through. let me know if solves issue.


Comments

Popular posts from this blog

sql - text truncated using perl DBI insert -

c++ - Is it possible to compile a VST on linux? -

php - Pass object by reference or value -