[Rails] accepts_nested_attributes_for how, example
I have following tables with following models:
users(id, role_id) has_many :entries
categories(id, category_name) has_many :entries
entries(id, category_id, user_id) belongs_to :user, belongs_to :category, has_one :storage
storages(id, title, content, entry_id) belongs_to :entry, has_one :vote
votes(id, count, storage_id) belongs_to :storage
Now, when user create new entry(through form) few things must happen:
- New row in entry table will be created(with already existing user_id and category_id)
- New row in storages table will be created(title and content of the post with above entry_id)
- New row in votes table(with above storage_id, count is default 0)
Form for creating new entry have, combo box for selecting category(like: "Pictures", "Movies", etc), text box for title and text-area for content.
I followed many tutorials and documentation like:
http://railscasts.com/episodes/196-nested-model-form-part-1 and
http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html#method-i-accepts_nested_attributes_for
but can't get it how to work.
-- users(id, role_id) has_many :entries
categories(id, category_name) has_many :entries
entries(id, category_id, user_id) belongs_to :user, belongs_to :category, has_one :storage
storages(id, title, content, entry_id) belongs_to :entry, has_one :vote
votes(id, count, storage_id) belongs_to :storage
Now, when user create new entry(through form) few things must happen:
- New row in entry table will be created(with already existing user_id and category_id)
- New row in storages table will be created(title and content of the post with above entry_id)
- New row in votes table(with above storage_id, count is default 0)
Form for creating new entry have, combo box for selecting category(like: "Pictures", "Movies", etc), text box for title and text-area for content.
I followed many tutorials and documentation like:
http://railscasts.com/episodes/196-nested-model-form-part-1 and
http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html#method-i-accepts_nested_attributes_for
but can't get it how to work.
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/76302b6f-b257-440f-862c-1f11ea8929e7%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home