No one seems to be able to help with my request. I bet this can be done
in rails, but I’m not going to figure it out without any help anytime
soon.
Trying to be brief: I have three classes: Account, Employee, and
AccountEmployeeRelation (which contains more than just the FKs).
In the form where I create a new account, I know how to add a ONE linked
employee. I do not know how to extend this to add MULTIPLE linked
employees.
Here is the working code for one employee:
def new
@account = Account.new
@relation = AccountEmployeeRelation.new
end
def create
@account = Account.new(params[:account])
@relation = AccountEmployeeRelation.new(params[:relation])
@account.account_employee_relations << @relation
@account.save
@relation.save
redirect_to :action => :list
end
The View:
<% form_for :account, :url => { :action => :create } do |form| %>
Title: <%= form.text_field :title %>
<% fields_for :relation do |f|%>
Employee: <%= f.select (:employee_id, Employee.find(:all).map{|u|
[u.name, u.id]}) %>
<% end %>
<%= submit_tag %>
<% end %>
Can anyone help with extending this? I think new needs to look like:
def new
@relations = []
3.times do # I would not really do it this way.
@relations << AccountEmployeeRelation.new
end
end
But I’m stuck on the view.