Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Models in subfolders and 'You cannot call create unless the parent is saved' error


  • Please log in to reply
No replies to this topic

#1 kabanek

kabanek

    Passenger

  • Members
  • 2 posts

Posted 16 March 2014 - 10:14 PM

Hello all!

 

I have a simple model

class Order < ActiveRecord::Base
    has_many :products, dependent: :destroy, class_name: 'Order::Product'
    has_many :attributes, dependent: :destroy, class_name: 'Order::Attribute'
    belongs_to :customer
    has_one :invoice_address, class_name: 'Order::Address'
    has_one :delivery_address, class_name: 'Order::Address'

    validates :number, presence: true
    validates :total_amount, presence: true
end

I wanted to add products and attributes to orders. I created 2 new files

models/order/product.rb

class Order::Product < ActiveRecord::Base
    belongs_to :order
end

and models/order/attribute.rb

class Order::Attribute < ActiveRecord::Base
    belongs_to :order
end

I created seeds file with code:

dev_customer = Customer.create(:name => 'dev')

first_order = dev_customer.orders.create(:total_amount => 555, :paid_amount => 555)

first_order.products.create(:name => 'First product', :price => 111, :qty => 1)
first_order.products.create(:name => 'Second product', :price => 222, :qty => 2)

but when I execute rake db:setup I receive error

 

You cannot call create unless the parent is saved

 

on line 5. What do I do wrong?

 

On rspec tests I receive a different error

 

$ rspec

/home/bartek/.rvm/gems/ruby-2.1.1/gems/activesupport-4.0.3/lib/active_support/dependencies.rb:464:in `load_missing_constant': Unable to autoload constant Product, expected /home/bartek/rails_apps/allemag/app/models/order/product.rb to define it (LoadError)

 

Here is my config/application.rb file

require File.expand_path('../boot', __FILE__)

require 'rails/all'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env)

module Allemag
  class Application < Rails::Application
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.

    # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
    # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
    # config.time_zone = 'Central Time (US & Canada)'

    # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
    # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
    # config.i18n.default_locale = :de

    config.autoload_paths += Dir[Rails.root.join('app', 'models', '{**}')]
    config.autoload_paths += %W(#{config.root}/lib)
    config.i18n.enforce_available_locales = true
  end
end

What do I do wrong? I think that rails has problems with namespaces, but have no idea how to fix it






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users