0

I have two model as:

Customer:

has_many :products, :dependent => :destroy
accepts_nested_attributes_for :products, reject_if: proc { |attributes| attributes['product_id'].blank? }

Product:

belongs_to :customer

products controller:

def product_params
    params.require(:product).permit(:name, :first_build)
end

customers controller:

    def customer_params
        params.require(:customer).permit(:first_build, :name, :product_id, 
                                  products_attributes: [:first_build, :customer_id])
    end

So in the customers controller I do this

@customer.products.build(:first_build => true)

but I get this error

unknown attribute 'first_build' for Prodcut

but when I do this @customer.products.build(:name => "test product name")

it works perfectly without any error. One thing to note here, first_build is not a column in the products table.

3
  • I assume you meant to type Product not Prodcut... but if first_build is not an attribute (not a column) why are you passing it? Commented Apr 18, 2016 at 11:45
  • Yes it is Product. I just want to know if it is possible to pass attributes that are not in the table. Commented Apr 18, 2016 at 12:44
  • Oh, yes you can. I'll post an answer. Commented Apr 18, 2016 at 13:18

1 Answer 1

1

If you want to pass attributes that are not in the table, you can create a "temporary" attribute which will not be stored in the table but will be available while the object is in memory.

class Product < ActiveRecord::Base

  attr_accessor :first_build
  ...

end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.