Ruby-on-Rails [SOLVED]: undefined method `prouct_price?' for class OrderItems–(OrderItemsController#create)

Ruby-on-Rails [SOLVED]: undefined method `prouct_price?' for class OrderItems–(OrderItemsController#create)

Home Forums Frameworks Ruby on Rails Tutorials Ruby-on-Rails [SOLVED]: undefined method `prouct_price?' for class OrderItems–(OrderItemsController#create)

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #246758

    Cloudy Point
    Keymaster

    QuestionQuestion

      CreateOrderItems Migration
      def change
        create_table :order_items do |t|
          t.integer :product_id
          t.integer :order_id
          t.decimal :unit_price
          t.integer :p_quantity
          t.decimal :total_price
    
          t.timestamps
        end
      end
    	OrderItemsController
      def create
    		@order = current_order
    		@order_item =               @order.order_items.new(order_item_params)
    		@order.save
    		session[:order_id] = @order.id
    	end
      	def order_item_params
    		params.require(:order_item).permit(:product_id, :p_quantity)
    	end
    	OrderItem Model
      belongs_to :product
      def unit_price
    		if persisted?
    			self[:unit_price]
    		else
    			Product.product_price?
    		end
    	end

    I have defined association, but Create method of OrderItem is unable to fetch product_id. Product has many order item relation, please answer, what mistake i’m doing..?
    Thank you

    create_table "products", force: :cascade do |t|
        t.string "product_title"
        t.text "key_features"
        t.decimal "product_price"
        t.string "colour"
        t.string "main_material"
        t.integer "product_quantity"
        t.datetime "created_at", null: false
        t.datetime "updated_at", null: false
      end

    #246759

    Cloudy Point
    Keymaster

    Accepted AnswerAnswer

    It is just a simple spelling error.
    In the method unit_price of OrderItem Model, you are using Product.prouct_price .

    Instead of that you should use product.product_price. First error was of spelling mistake and second error is because you are using Product instead of product for using associated product to OrderItem.

    Also, I am unable to understand that why have you kept unit_price and total_price in table of OrderItem as you can dynamically evaluate or figure out that value using associations and simple math. So, according to me it is unnecessary to make colums for these values.

    Source: https://stackoverflow.com/questions/47966069/undefined-method-prouct-price-for-class-orderitems-orderitemscontrollercre
    Author: Aakash Gupta
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.