Edit a model with has_many and at the same time adding a new object associated.

50 views
Skip to first unread message

Mauro

unread,
Jul 1, 2011, 6:46:53 AM7/1/11
to rubyonrails-talk
I have Shop has_many :documents and Documen belongs_to :shop.
When I create a new shop I use accept_nested_attributes_for :documents
in the model and fields_for in the form, so I can create a new shop
and at the same time a document associated to the shop.
I want to add a new document associated to the shop also when I edit
the shop, at the same time I edit the shop.
If I call the edit action for shop, in the form I have the fields for
one of the documents associated with their values but I want to add a
new document.
There is a way to do this?

Chirag Singhal

unread,
Jul 1, 2011, 6:51:43 AM7/1/11
to rubyonra...@googlegroups.com
Checkout these two railscasts, they should get you started in right direction:


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.


Mauro

unread,
Jul 1, 2011, 3:59:01 PM7/1/11
to rubyonra...@googlegroups.com
On 1 July 2011 12:51, Chirag Singhal <chirag....@gmail.com> wrote:
> Checkout these two railscasts, they should get you started in right
> direction:
> http://railscasts.com/episodes/196-nested-model-form-part-1
> http://railscasts.com/episodes/197-nested-model-form-part-2

I've found that is more simpler than I've thought.

Models are:

Shop has_many :document, Document belongs_to :shop.

For edit action I've done:

@shop = Shop.find(params[:id])
@document = @shop.documents.new

and in the form I've done:

fields_for @document.

Now I can modify an existing shop and at the same time add a document.
Do you think it's all correct?

vinny

unread,
Jul 1, 2011, 4:10:42 PM7/1/11
to rubyonra...@googlegroups.com
I'm assuming this is rails 3.

For your edit action you don't need the @document variable.  You can refer to all of your documents via the :has_many association you created in the model so @shop.documents will suffice.

Your fields_for will need to look something like this
fields_for :documents, @shop.documents |ff_item|
  stuff for whatever you need (text area's checkboxes etc.)
end

If you want to access your data from your document in the fields_for loop, you can do "ff_item.object.DOCUMENT_METHOD".
ff_item.object will be an instance of your document so you can set the default values for your html items.

Vinny
@agnellvj

Mauro

unread,
Jul 2, 2011, 7:24:05 AM7/2/11
to rubyonra...@googlegroups.com
On 1 July 2011 22:10, vinny <agne...@gmail.com> wrote:
> I'm assuming this is rails 3.
> For your edit action you don't need the @document variable.  You can refer
> to all of your documents via the :has_many association you created in the
> model so @shop.documents will suffice.

The problem is that I want to add a new document when I am in the edit action.
In the form for edit I have all shop fields with their values and also
the document fields with their values.
I want the document fields blank so that I can insert new values and
add a document for the shop.

Reply all
Reply to author
Forward
0 new messages