Note that there are some explanatory texts on larger screens.

plurals
  1. POValidation methods in plugins
    text
    copied!<p>I am using Ruby on Rails 3.0.7 and I am trying to implement an <code>act_as_article</code> plugin for my application. What I would to do is run <em>validation</em> methods for the "acting as article class" inside that plugin (note: my plugin requires to create some database table columns in order to work - one of these is represented by the <code>title</code> attribute).</p> <p>In my RoR application I have this code:</p> <pre><code># vendor/plugins/article/lib/acts_as_article.rb module Article extend ActiveSupport::Concern included do validates :title, # Validation method :presence =&gt; 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 </code></pre> <p>Using the above code the plugin works. However if I add some <em>Record Association</em> in the <code>Review</code> class like this:</p> <pre><code>class Review acts_as_article has_many :comments # Adding association ... end </code></pre> <p>and in my <code>ReviewsController</code> I add the following, as well:</p> <pre><code>def create ... @article.comments.build( # This is the code line 89 :user_id =&gt; @user.id ) if @article.save ... end end </code></pre> <p>I get this error</p> <pre><code>NoMethodError (undefined method `title' for #&lt;Comments:0x00000103abfb90&gt;): app/controllers/articles_controller.rb:89:in `create' </code></pre> <p>Probably it happens because the validation run for all <code>Review</code> "associated" classes\models and the <code>Comment</code> class <em>doesn't have</em> the <code>title</code> attribute. I think that because if inside the plugin code I comment out the validation method like this</p> <pre><code>module Article ... included do # validates :title, # Validation # :presence =&gt; true end ... end </code></pre> <p>I don't get errors anymore.</p> <p>So, <strong>how can I solve this issue?</strong></p> <p><em>Note</em>: I am not expert on creating plugin (this is my first time), so I ask implicitly also if I'm doing a good job for the plugin implementation...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload