Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8647129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:59:19+00:00 2026-06-12T12:59:19+00:00

I am developing a Rails application which uses a many-to-many relationship, which needs extra

  • 0

I am developing a Rails application which uses a many-to-many relationship, which needs extra attributes on the join table (apart from the ids of the two related entities).

In the model, a Product can belong to many Orders, and an Order can have many Products.
So I created an action for assigning Products to a given Order, called add_product_order, and the controller method that handles the form when it’s sent is finish_add_product_order:

def finish_add_product_order
  @order = Order.find(params[:id])
  @product = Product.find(params[:order][:products])

  if @op = OrdersProduct.find_by_order_id_and_product_id(@order.id, @product.id)
    @op.quantity = params['quantity'][0]
  else
    @op = OrdersProduct.new(:order => @order, :product => @product, :quantity => params['quantity'][0])
  end

  respond_to do |format|
    if @op.save
      format.html { redirect_to @order, notice: 'Order was successfully updated.' }
      format.json { head :no_content }
    else
      format.html { render action: "add_product_order", notice: 'No se ha podido grabar.' }
      format.json { render json: @order.errors, status: :unprocessable_entity }
    end
  end
end

The orders_products table (which is used for storing the related ids and quantities) has the following index:

add_index :orders_products, [:order_id, :product_id], :unique => true

The action above (finish_add_product_order) works fine if the Product isn’t already in the Order (i.e. OrdersProduct.new is being called). But this is the error I get when it already exists and I try to save it:

SQLite3::SQLException: no such column: orders_products.: UPDATE "orders_products" SET "quantity" = 4 WHERE "orders_products"."" IS NULL

The request parameters are the following, just in case:

{"utf8"=>"✓",
 "_method"=>"put",
 "authenticity_token"=>"TwPMk73MhCM2IeMfmf2g/fdm3+ahpyaxs1InULC/8ig=",
 "order"=>{"products"=>"4"},
 "quantity"=>["4"],
 "commit"=>"Update Order",
 "id"=>"2"}

I believe the WHERE clause should include both the order and the product id so as to identify the row affected, but I don’t know why the column names are not being included, neither why it ends with “IS NULL”.

Any suggestions? Thanks

EDIT

This is the code for the three classes involved:

class Order < ActiveRecord::Base
  belongs_to :user

  has_many :gardens, :dependent => :destroy

  has_many :products, :through => :orders_products
  has_many :orders_products

  attr_accessible :address, :delivery_date, :paid, :user, :orders_products_attributes

  # hid some irrelevant methods
end



class Product < ActiveRecord::Base
  has_many :gardens, :through => :gardens_products
  has_many :gardens_products

  has_many :orders, :through => :orders_products
  has_many :orders_products

  attr_accessible :cost, :description, :stock, :title, :x_size, :y_size, :pivot_x_position, :pivot_y_position, :is_gallery_product

  validates_presence_of :cost, :stock, :title, :x_size, :y_size, :pivot_x_position, :pivot_y_position
  validates_numericality_of :cost, :stock, :x_size, :y_size, :pivot_x_position, :pivot_y_position, :only_integer => true, :message => "Solo puede ser un numero entero"
  validates_inclusion_of :cost, :in => 0..1000000, :message => "Solo puede estar entre 0 y 1 000 000"
  validates_inclusion_of :stock, :in => 0..10000000, :message => "Solo puede estar entre 0 y 10 000 000"
  validates_inclusion_of :x_size, :y_size, :in => 1..200, :message => "Solo puede estar entre 0 y 200"
  # Poner el rango variable en los pivotes (dentro del rango del tamano)
  validates_inclusion_of :pivot_x_position, :in => 1..200, :message => "El pivote debe estar dentro de las dimensiones del producto"
  validates_inclusion_of :pivot_y_position, :in => 1..200, :message => "El pivote debe estar dentro de las dimensiones del producto"
  validates :title, :length => { :minimum => 5},
                            :presence => { :message => "Debe tener un titulo de largo mayor que 5 caracteres" }
end


class OrdersProduct < ActiveRecord::Base
  belongs_to :order, :dependent => :destroy
  belongs_to :product

  attr_accessible :order, :product, :quantity

  validates_numericality_of :quantity,
                            :only_integer => true,
                            :message => "solo puede ser un numero entero"
  validates_inclusion_of :quantity,
                            :in => 1..1000,
                            :message => "solo puede estar entre 1 y 1 000"
end

Thanks again

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T12:59:21+00:00Added an answer on June 12, 2026 at 12:59 pm

    Since the orders_products table had been created without an id (with create_table :orders_products, :id => :false do |t|...), all I had to do was to add an id to it, with this migration:

    class AddIdToOrdersProduct < ActiveRecord::Migration
      def change
        add_column :orders_products, :id, :primary_key
      end
    end
    

    that way, it worked after doing rake db:migrate.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing Ruby on Rails application which uses Thinking Sphinx. Unfortunately, from time
I am developing a Rails 3 application from which I want to be able
I am developing Rails application, in which I need to authorize users from other
I'm developing a new rails application which is supposed to be installed several times
In the Rails application I’m currently developing I have many request models. We are
I'm developing a Ruby on Rails application that needs to allow the user to
I am developing a Rails application which will need frequent access to public APIs,
I am developing a gem for my Rails application, which be loaded into it
I have a Ruby on Rails application that I'm developing on my computer, which
I am developing a Rails 3 application (which acts as an API) and I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.