Copy fixed data into model
I'm developing an application in which a Course belongs to a user. I would like to predefine a number of courses, such that a Course's template details are then copied into the users course details. From this initial point each user has a one-to-one mapping with a course.
I'd like to know the best place fo开发者_开发问答r the static attributes for building a user's course.
Thanks, Adam
You could use a before_create or after_create filter on your user model, something like this:
before_create :add_default_courses
def add_default_courses
self.courses << Course.new({:foo => 'bar'});
end
you can initialize courses after a user is created.
User.rb #you user file
def after_initialize
self.courses << add_courses
end
private
def add_courses
@add_courses = Courses.find(:all, conditions => [])
end
精彩评论