I am new to RoR and still playing with associations. I need to have two references to a particular model in another model. The scaffolded code doesn't work and I get a "uninitialized constant" error.
Generation commands:
script/generate scaffold BaseModel name:string
script/generate scaffold NewModel name:string base1:references base2:references
db:migrate
Generated models:
class NewModel < ActiveRecord::Base
belongs_to :base1
belongs_to :base2
end
and
class BaseModel < ActiveRecord::Base
has_many :new_models # I added this line
end
When I try to create a new_model at /new_models/new
, I tried both the ID and the name of the BaseModel but it does not work. The error I get is:
uninitialized constant NewModel::Base1
I guessed it maps the names, so in my create method, I tried to explicitly set the BaseModel instances:
@new_model = NewModel.new(params[:new_model])
@base1 = BaseModel.find(1) # this exists
@base2 = BaseModel.find(2) # this exists
@new_model.base1 = @base1 # This throws the same error as above
Is there anything I'm missing?