Rails 3.2 Пользовательский внешний ключ вложенной формы невозможно сохранить

У меня есть эта модель Отзывы:

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

  belongs_to :business
  belongs_to :product
end

и эта модель продукта:

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

и эта бизнес-модель:

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

но с этим создать действие в контроллере Reviews:

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

Я не могу спасти ассоциацию. simple_form говорит, что есть ошибка, но я не вижу ошибки в своих журналах, и все остальные поля проверены, поэтому я предполагаю, что есть проблема с ассоциацией.

ПОБОЧНЫЙ ВОПРОС: как я могу заставить simple_form показывать все ошибки?

1 ответ

Решение

Исправил это, используя полиморфные ассоциации и избавившись от for и updated_id в модели обзоров. обратитесь сюда:

http://guides.rubyonrails.org/association_basics.html

Другие вопросы по тегам