Question

I have this Reviews Model:

class Review < ActiveRecord::Base
  attr_accessible :approved, :reviewed_id, :for, :user_id, :text, :rating, :title

  belongs_to :business
  belongs_to :product
end

and this Product model:

class Product < ActiveRecord::Base
  include ApplicationHelper
  belongs_to :business
  belongs_to :catalog
  belongs_to :category

  has_many :reviews, :foreign_key => :reviewed_id
  has_many :features, :dependent => :destroy
end

and this Business model:

class Business < ActiveRecord::Base
  validates_presence_of :name
  validates_presence_of :category

  mount_uploader :photo, PhotoUploader

  has_many :catalogs, :dependent => :destroy
  has_many :products, :dependent => :destroy
  has_many :branches, :dependent => :destroy
  has_many :reviews, :foreign_key => :reviewed_id
end

but with this create action in the Reviews controller:

def create
  @business = Business.find(params[:business_id])

  if params.has_key?('product_id')
    @product = Product.find(params[:product_id])
    @review = @product.reviews.build(params[:review])

    if @review.save
      flash[:notice] = 'Pending Review Submitted'
      redirect_to business_product_path(@business, @product)
    else
      @form_resources = [@business, @product, @review]
      respond_with(@business, @product, @review)
    end
  else
    @review = @business.reviews.build(params[:review])

    if @review.save
      flash[:notice] = 'Pending Review Submitted'
      redirect_to business_path(@business)
    else
      @form_resources = [@business, @review]
      respond_with(@business, @review)
    end
  end
end

I can't save the association. simple_form says there is an error, but I can't see the error in my logs, and all the other fields are validated so I'm guessing there is a problem with the association.

SIDE QUESTION: how can you I make simple_form show all errors?

Was it helpful?

Solution

fixed it by using polymorphic associations and getting rid of for and reviewed_id in the reviews model. refer here:

http://guides.rubyonrails.org/association_basics.html#polymorphic-associations

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top