开发者

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜