ruby on rails - Has_one association confusion, I want it reversed -


i'm pretty new rails , lately found understood activerecords has_one association contrary how works. refering example rubyonrails guide imagined supplier should hold account_id, since see no point in forcing every account hold supplier.

because don't understand why, or don't agree object maintaining it's foreign keys in other objects, don't know correct rails solution simple problem: have 2 objects - document, , draft. every document has many drafts, , 1 of them marked current draft. imagined table layout this:

table document     id     current_draft_id table draft     id     document_id     text 

what i'm looking here has_one association, reversed, document held , maintain current_draft_id. using belongs_to not option because of different behaviour. example i'd document.current_draft = new_draft update foreign_key correctly.

how approach in rails?

-- update 1

to clarify problem, please assume draft being current won't have nothing created_at , updated_at fields, scopes won't do.

adding current field drafts table weird move table design point of view. i'm planning add information published draft document object, , multiplying such informations in drafts tableseems odd step.

i amesee's idea, still have inner resistances, similar adding current column drafts table.

i argue draft document may make more sense manage these classes single table inheritance. can tell draft "current draft" type.

class createdocuments < activerecord::migration   def change     create_table :documents |t|       t.string :type       # ...        t.timestamps     end   end end 

and models.

class document < activerecord::base   # ... end  class draft < document   # ... end  class currentdraft < draft   # ... end 

later on, when draft isn't "current" anymore, update type changing type attribute "draft" or "document." think better solution checking boolean or date attribute on object , asking state everywhere in application.


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 -