I have an app where the user can build a project which will have up to 12 volunteer time slots where other users can sign up to work on the project.
When a project is created, the user is asked for the default (beginning) time. This default time (Project_Times model) should be written to the begin_time in the Projects table.its own table in the database, using the project_id parameter to identify which project it belongs to.
The user can then add up to 11 more time slots if they want/need to after the @project is created.
I am having a hard time getting the project to create the default time. This is what I have:
++ Projtime (projtime.rb) Model ++
class Projtime < ActiveRecord::Base
attr_accessible :start_time, ...
belongs_to :project
default_scope :order => 'times.amount ASC'
end
++ Project (project.rb) Model ++
class Project < ActiveRecord::Base
attr_accessible :title
belongs_to :user
has_one :project_category
has_many :projtimes, :dependent => :destroy
accepts_nested_attributes_for :projtimes
def projtimes
Projtimes.where('project_id=?', id)
end
end
++ Projects Controller ++
class ProjectsController < ApplicationController
...
def create
@user = current_user
@project = current_user.build_project(params[:project])
@project.save
@render 'edit'
end
where the project is automatically assigned to the user using the syntax above. I have tried to copy the same concept (factoring in the fact that there are multiple times for each project) and come up with this for the ProjtimesController:
class ProjtimesController < ApplicationController
...
def create
@project.projtimes.build
end
end
However, I’m getting a NameError in ProjectsController#new
with uninitialized constant Project::Projtimes
I would think that with setting the belongs_to and has_many associations, this should work.
Help anyone?
projtime.rb should start with
class Projtimenotclass Projime.A second set of eyes 🙂