Note that there are some explanatory texts on larger screens.

plurals
  1. PORails behaving strange
    primarykey
    data
    text
    <p>I have rails version <code>3.2.13</code> and ruby version <code>1.9.3</code>.</p> <p>I have caught into very strange and interesting situation.</p> <p>In my application have a model 'Product' with custom validator.</p> <p><strong>product.rb</strong></p> <pre><code>class Product &lt; ActiveRecord::Base attr_accessible :description, :name, :price, :short_description, :user_id validates :name, :short_description, presence: true validates :price, :numericality =&gt; {:greater_than_or_equal_to =&gt; 0} validate :uniq_name belongs_to :user belongs_to :original, foreign_key: :copied_from_id, class_name: 'Product' has_many :clones, foreign_key: :copied_from_id, class_name: 'Product', dependent: :nullify def clone? self.original ? true : false end private #Custom validator def uniq_name return if clone? user_product = self.user.products.unlocked.where(:name =&gt; self.name).first errors[:name] &lt;&lt; "has already been taken" if user_product &amp;&amp; !user_product.id.eql?(self.id) end end </code></pre> <p>In products controller's create action when I am trying to create new product </p> <pre><code>def create @product = current_user.products.new(params[:product]) respond_to do |format| if @product.save format.html { redirect_to @product, notice: 'Product was successfully created.' } format.json { render json: @product, status: :created, location: @product } else @product.errors[:image] = "Invalid file extension" if @product.errors[:image_content_type].present? format.html { render action: "new" } format.json { render json: @product.errors, status: :unprocessable_entity } end end end </code></pre> <p>Custom validator is being called when this line executed <code>@product = current_user.products.new(params[:product])</code> and <code>line # 2</code> of custom validator giving me error</p> <pre><code>undefined method `products' for nil:NilClass </code></pre> <p>I have inspected product object in custom validator but <code>user_id</code> is nil. Why <code>user_id</code> is not being autoassigned?</p> <p>Your help will be appreciated :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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