Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8973987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:34:28+00:00 2026-06-15T18:34:28+00:00

I have a model artists and now I would like create a model user_artist

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T18:34:30+00:00Added an answer on June 15, 2026 at 6:34 pm

    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

    def create
       @artist = Artist.new(params[:artist])
       if @artist.save
           @user_artist = UserArtist.new(:artist_id => @artist.id, :user_id => current_user.id)
           if @user_artist.save
              #code...
           else
              #errors...
           end
       else
           #errors...
       end
    end
    

    (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):

    @artist = Artist.new(params[:artist])
    @artist.user_artists.new(:user_id => current_user.id)
    if @artist.save
       #code
    else
       #errors
    end
    

    Also, I found this interesting entry: Save changes to a has_many association ONLY when you successfully save the parent object?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model that looks like the following: class LibraryEntry(models.Model): host_lib_song_id = models.IntegerField()
I have a user model that has_one profile, and the profile belongs to the
I have a simple model like this one: class Artist(models.Model): surname = models.CharField(max_length=200) name
I have model Foo which has field bar. The bar field should be unique,
I have model Article it has field title with some text that may contain
I have model like this: scope :search_posts, lambda { |query| mega_posts.where( title LIKE ?
If you have a controller method like so: @expose(json) def artists(self, action=view,artist_id=None): artists=session.query(model.Artist).all() return
I'm trying to model artists and songs and I have a problem where I
I have two models that are associated. I have an Artist model and a
I'm figured out with no results. I have a model named User and to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.