0

I m a newbie on Rails I have User and Attachment model and has_many through relationships. I want to add data,it can be more than one, to third model which is intermediate form when I create the attachment data. I can save the attachment data but I don't know how I save data to third model.Here my files

attachments/new.html.erb

<%= form_for(@attachment) do |f| %>
<div class="field">
<%= f.label :filename, "File Name" %><br>
<%= f.text_field :filename %>
</div><br />
<div class="field">
<%= f.label :file, "File" %><br>
<%= f.file_field :file %>
</div> <br />  
<div class="actions">
<%= f.submit "Dosya yükle" %>
</div>
<% end %>

attachments_controller.rb

  def new
  @attachment = Attachment.new
  end
  def create
  @attachment = Attachment.new(file_params)
  if @attachment.save
     redirect_to some_path
  else
     render action: 'new' 
  end
  end

user.rb

  has_many :interforms
  has_many :attachments, through: :interforms

attachment.rb

  has_many :interforms
  has_many :users, through: :interforms

interform.rb

  belongs_to :user
  belongs_to :attachment

My question is how I add data to interform when I add to attachment

Edit : I didn't have any routes for interforms right now

Ramazan Zor
  • 209
  • 1
  • 14
  • 1
    please invest sometime and explain your problem through code or something you have tried, this way people will understand your problem better. – Abhinay Aug 19 '15 at 13:55
  • Sorry it is my first question here, I will edit the question – Ramazan Zor Aug 19 '15 at 14:00
  • I sense that you need to read about [build](http://stackoverflow.com/a/15465680/2968762) – Abhi Aug 19 '15 at 14:13
  • I don't know where to call that method, I will try in attachments create method but did not work, I do not know whether I can do it in form or not – Ramazan Zor Aug 19 '15 at 14:22
  • either you can use `build` as mentioned by Abhi or you can simply store them as usual something like: `Interform.create(user_id: params[:user_id], attachment_id: params[:attachment_id], ....... )` It will still work.I hope thats what you are asking for. PS: wont recommend later one but if you are just doing it for the sake of learning then either is good, `build` is best practice. – Abhinay Aug 19 '15 at 15:05
  • I just wonder, do i use build in attachment create method?, because when i create the attachment item, i also want to create association – Ramazan Zor Aug 19 '15 at 16:23

0 Answers0