Playing a lot with nested_attributes code and documents, finally with a working program that also checks the association. These are the changes (indicated between ** .... **) listed below
class Item < ActiveRecord::Base has_many :ratings, dependent: :destroy, **inverse_of: :item** accepts_nested_attributes_for :ratings, :allow_destroy => true validates :name , :length => { minimum: 3 } validates :category , :length => { minimum: 3 } end class Ratings < ActiveRecord::Base belongs_to :user belongs_to :item, **inverse_of: :ratings** default_scope -> { order('created_at DESC') } validates :user_id, :presence => true validates_presence_of :item validates_numericality_of :rating, :greater_than_or_equal_to => 0 validates_numericality_of :rating, :less_than_or_equal_to => 5 end
Still unable to create one of @item = Item.create(params[:item]) which still gives ActiveModel::ForbiddenAttributesError as suggested by @BroiSatse, as well as nested_attributes documents, which should not be in this case
the problem may be in
class ItemsController < ApplicationController def item_params params.require(:item).permit(:name, :url, :full_address, :city, :country, :category, :ratings_attributes => [:rating, :comment]) end
will work to resolve this and send a response if I find a solution.
source share