Jared Brown
Jared Brown

Reputation: 1921

Using relationships between Rails models in folders

Error

uninitialized constant Suspicious::Activity::SuspiciousPerson

Model structure

app
  models
    suspicious
      activity.rb
      person.rb

Model classes

class Suspicious::Activity < ActiveRecord::Base
  has_many :suspicious_people, :dependent => :destroy
  accepts_nested_attributes_for :suspicious_people, :allow_destroy => true

class Suspicious::Person < ActiveRecord::Base
  belongs_to :suspicious_activity

This is where the error occurs [line 3]

1 def new
2   @activity = Suspicious::Activity.new
3   @activity.suspicious_people.build
4 end

Upvotes: 2

Views: 168

Answers (1)

Dogbert
Dogbert

Reputation: 222108

When you're namespacing models like this, you don't need to prepend 'suspicious_' when referencing to models with the same name.

Model

class Suspicious::Activity < ActiveRecord::Base
  has_many :people, :dependent => :destroy
  accepts_nested_attributes_for :people, :allow_destroy => true
end

class Suspicious::Person < ActiveRecord::Base
  belongs_to :activity
end

Controller

def new
  @activity = Suspicious::Activity.new
  @activity.people.build
end

Upvotes: 1

Related Questions