2

I have two models Plan and Student::Plan. When calling Student::Plan.create! in my seeds.rb file I get back a Plan object. It seems as though rails is getting confused by the namespacing or something. Any thoughts that don't involve renaming the class?

file structure

models
  plan.rb
  student
    plan.rb

tables

  • plans
  • student_plans

Update

This gets even weirder when testing w/ rails c. If you call Student::Plan.create! first then you'll get the expected object back. If you instead call Plan.create! and then Student::Plan.create! you'll be back a Plan object instead of what you'd actually expect.

Works

Student::Plan.create # <Student::Plan...>

Doesn't Work

Plan.create # <Plan...>
Student::Plan.create # <Plan...>
16
  • Where did you place Student::Plan? Commented Jun 11, 2013 at 16:27
  • @KenrickChien I've updated w/ some additional details Commented Jun 11, 2013 at 16:36
  • 1
    Does Student::Plan inherit from Plan? Are you referencing Plan within the Student module before Student::Plan is properly loaded? Calling these both Plan is trouble. Commented Jun 11, 2013 at 16:40
  • 1
    It seems like you're getting problems from Rails' autoloading of classes. Some possible solutions: 1. Move Student::Plan to lib. 2. place require 'student/plan' where you need to reference Student::Plan. Commented Jun 11, 2013 at 16:40
  • 1
    You may need models/student.rb to define module Student see: stackoverflow.com/questions/9712468/… and wondible.com/2011/12/23/… Commented Jun 11, 2013 at 16:54

1 Answer 1

1

I can imagine a scenario where you have namespaced models (I've used it a few times...usually for integrating different db's). And this answer doesn't get to the issue of dealing with namespacing issues, but I do think the "Judo" solution is to just create a model called student_plan.rb at the root of the models directory, and use StudentPlan.create and Plan.create respectively.

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.