I have a model artists and now I would like create a model user_artist that should save the current logged user and a reference to the Artist. I take a new model because there could be later more than one user in this database for one artist.
Now I have the model and how can I save now the current user during the user creates a new Artist.
Here is my current form:
<%= form_for @artist do |f| %>
<%= f.label :name, "Name" %>
<%= f.submit "Create" %>
<% end -%>
How I must change this to simultaneously save the user in the database user_artist?
If you want to save the current user logged, you can edit your controller, so, after saving the Artist you can save the UserArtist, something like this for example
(There is a simpler way to do this that I have never tried; I will search and show you that way, but if someone knows that please tell us)
Also, I didn’t tell you about creating a callback because in the model you can’t access session variables (I’ve just remember it)
For the future, when you want to add dinamically more UserArtist on one single form, check this out http://railscasts.com/episodes/196-nested-model-form-part-1 and http://railscasts.com/episodes/197-nested-model-form-part-2
UPDATE
This is the way I’ve told you before (it will save both records):
Also, I found this interesting entry: Save changes to a has_many association ONLY when you successfully save the parent object?