8

I have something like this:

module Api
  module V1
    class Order < ActiveRecord::Base

    has_many :order_lines
    accepts_nested_attributes_for :order_lines
  end
end

module Api
  module V1
    class OrderLine < ActiveRecord::Base

    belongs_to :order
  end
end

In my orders controller, I permit the order_lines_attributes param:

params.permit(:name, :order_lines_attributes => [
                      :quantity, :price, :notes, :priority, :product_id, :option_id
            ])

I am then making a post call to the appropriate route which will create an order and all nested order_lines. That method creates an order successfully, but some rails magic is trying to create the nested order_lines as well. I get this error:

Uninitialized Constant OrderLine.

I need my accepts_nested_attributes_for call to realize that OrderLine is namespaced to Api::V1::OrderLine. Instead, rails behind the scenes is looking for just OrderLine without the namespace. How can I resolve this issue?

1
  • maybe try adding class_name: "Api::V1::OrderLine" to has_many :order_lines? Commented Oct 14, 2015 at 19:34

1 Answer 1

1
+25

I am pretty sure that the solution here is just to let Rails know the complete nested/namespaced class name.

From docs:

:class_name

Specify the class name of the association. Use it only if that name can't be inferred from the association name. So belongs_to :author will by default be linked to the Author class, but if the real class name is Person, you'll have to specify it with this option.

I usually see, that class_name option takes the string (class name) as a argument, but I prefer to use constant, not string:

module Api
  module V1
    class Order < ActiveRecord::Base
      has_many :order_lines,
        class_name: Api::V1::OrderLine
    end
  end
end

module Api
  module V1
    class OrderLine < ActiveRecord::Base
      belongs_to :order,
        class_name: Api::V1::Order
    end
  end
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.