ruby on rails - Set id on a csv import for a has_and_belongs_to_many relationship -
i have 2 models in app: list.rb , contacts.rb, have has_and_belongs_to_many relationship. have following method in contacts_controller.rb:
def import contact.import(params[:file]) redirect_to contacts_path, notice: "contacts imported." end i calling method after creating list in list#create action. how can set/input list_id import method above, records created through csv file?
thank you!
you need first list, example in show method. make sure import member-route.
@list = list.find(params[:id]) you need modify import method, takes second parameter list.
def contact.import_for_list(file, list) # loop on csv lines # each line create contact element # , add new contact element list list.contacts << newly_created_contact # or create contact list object list.contacts.build(....) # depending how created objects need save them explicitly end and lastly call modified method
def import @list = list.find(params[:id]) contact.import_for_list(params[:file], @list) redirect_to contacts_path, notice: "contacts imported." end
Comments
Post a Comment