ruby - Implementing a model of a model in rails -
i starting out rails , have run little issue. trying implement way allow users create "pages" on can tack on widgets.
so have set model "pages", has database fields such name, description etc. has "has_many :widgets" association.
here stumped. have list of widgets users can use. things such "about me", "photo gallery", "comments", "videos", etc. can chose 1 of these, customize contents , add page.
i figure, when user picks , customizes widget add entry widgets table "belongs_to user_id". however, need specify widget user chose , custom contents entered.
how implement in rails? can define each widget type?
i can think of few ways, not sure 1 best.
- one option use model per widget. have aboutmewidget, videoswidget, etc. allow me customize each model whatever fields widget might need. however, doesn't seem efficient , require many models.
- another option define widget types somewhere (but where?) , simplify "widget_type" in widgets table. (therefore table contain "belongs_to :user" , "has_one :widget_type".) have have field data user entered (probably through xml?).
- last option can think of use ruby classes define widgets. when user customized widget, create object widget's class , store object.
is there better way?
if specific fields given widget implementation don't need searchable i'd tempted put them serialized field.
class page << ar has_many :embedded_widgets has_many :widgets, :through => :embedded_widgets end class widget << ar has_many :embedded_widgets end class aboutmewidget << widget class videoswidget << widget ... etc ... class embeddedwidget << ar belongs_to :page belongs_to :widget serializes :data end
Comments
Post a Comment