ruby on rails - Implementing model subclasses - the correct way -


i have event model , want create 2 subclasses of it: fixedevent , tasksevent

event model has these attributes:

uid       :string title     :string starts_at :datetime ends_at   :datetime 

fixedevent inherits event , has attribute:

all_day   :boolean 

tasksevent inherits event , has these attributes:

task_id   :integer occurrence :integer 

(occurrence attribute special way tasks say: task two/three/x times. occurrence represents occurrence of task (e.g. second time user performing task))

i spent whole day yesterday reading single table inheritance , polymorphic associations , i'm still not 100% sure what's correct way implement in rails.

single table inheritance leaves me lot of null values in database i'll end having 1 table with: uid, title, starts_at, ends_at, all_day, task_id, occurence, type. behaviour make server response slower rails fetch more (null) data every event query?

on other hand, polymorphic associations more i'm adding functionality model, rather subclassing it. creates more tables (three in case) in db:

events: id, uid, title, starts_at, ends_at, created_at, updated_at, event_type_id, event_type_type 

(suggest better naming type if comes mind)

fixed_events: id all_day created_at updated_at  tasks_events: id task_id occurrence created_at updated_at 

will behaviour make server response slower rails have several db joins every time want fetch fixedevent/tasksevent attributes?

also, how can create new activerecord objects using sti and/or polymorphic associations? tried polymorphic association:

def new   @fixed_event = fixedevent.new   @fixed_event.build_event    respond_to :html end 

and in form_for:

= f.fields_for :event |event|   .field     = event.label :title     = event.text_field :title   .field     = event.label :starts_at     = event.datetime_select :starts_at   .field     = event.label :ends_at     = event.datetime_select :ends_at   .field     = event.label :description     = event.text_field :description   .field     = event.label :uid     = event.text_field :uid 

i had add these 2 things in fixedevent , tasksevent , worked:

attr_accessible :event_attributes accepts_nested_attributes_for :event 

is correct way or sti better (or other solution)?

you want go sti simple , faster loading associations. loading bunch of null values nothing concerned about, performance wise. on other hand, joins (or eager loading) more cause performance problems once events table contains tens of thousands of entries.

if selecting of columns ever becomes problem you, can select subset of columns with:

fixedevent.select('foo, bar').all 

it seems understand sti don't have teach how here. it's simple, really—just create "events" table "type" column, , event class, subclass it.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -