D
D
dexdev2014-10-13 21:35:18
Ruby on Rails
dexdev, 2014-10-13 21:35:18

Is it possible to add data to a table with a belongs_to relationship?

Is it possible to do this at all? and how to do it? )
Let's say there are posts and comments
Post.rb
has_many "comments
Comment.rb
belongs_to :post
and a form

<%= form_for([@post, @article.comments.build]) do |f| %>
  <p>
    <%= f.label :commenter %><br>
    <%= f.text_field :commenter %>
  </p>
  <p>
    <%= f.label :body %><br>
    <%= f.text_area :body %>
  </p>
f.fields_for :post do |f| %>
<%= f.text_field :body2 %>
<% end %>
  <p>
    <%= f.submit %>
  </p>
<% end %>

It is necessary that when creating a comment, the value of the input body2 goes into the post label.
How to do this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vsuhachev, 2014-10-14
@vsuhachev

The simplest thing is to open a transaction when saving objects, parse the parameters and write down everything you need and where you need

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question