型号
组.rb
class Group < ActiveRecord::Base
validates_presence_of :name
has_many :activities, :through => :group_activity
end
activity.rb
class Activity < ActiveRecord::Base
validates_presence_of :name
belongs_to :group
has_many :occurances, :through => :activity_occurance
end
occurance.rb
class Activity < ActiveRecord::Base
validates_presence_of :date
belongs_to :activity
end
迁移(单独或一起)
add_everything.rb
class AddEverything < ActiveRecord::Migration
def self.up
create_table :groups, :force => true do |t|
t.string :name
t.timestamps
end
create_table :group_activity, :force => true do |t|
t.integer :group_id, :activity_id
t.timestamps
end
create_table :activities, :force => true do |t|
t.string :name
t.timestamps
end
create_table :activity_occurance, :force => true do |t|
t.integer :activity_id, :occurance_id
t.timestamps
end
create_table :occurance, :force => true do |t|
t.datetime :date
t.timestamps
end
end
def self.down
drop_table :groups
drop_table :activities
drop_table :occurances
drop_table :group_activity
drop_table :activity_occurance
end
end
这会照顾您的模型工作。在您的组 _form 视图中,我将添加您的关联组名称,并为您的活动名称添加 fields_for 和为发生事件添加 fields_for。在您出现时,使用这个方便的 jQuery 日期时间选择器(它是 jQuery 日期选择器的扩展)来填充您的出现字段:
http://puna.net.nz/timepicker.htm
您还应该有单独的视图,以使用其各自的表单单独管理活动。在您的显示页面中显示其他字段是非常标准的,但对于出现的情况,您可以使用类似(haml 语法):
= @group.name
- for activity in @group.activities
= activity.name
- for occurance in activity.occurances
= occurance.date.strftime("%A at %r")
希望这至少能让你开始。如果要显示 day1/day2/day3 的内容,可以添加额外的逻辑来检查 activity.occurances.size 以相应地格式化