Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I automatically add a row to an existing table on creation of new user in ruby on rails?
    text
    copied!<p>I would like to add a row to <code>photo_albums</code> table in my db containing the user ID for <code>current_user</code> and an album title "Posts Album" <code>before_create</code>.</p> <ul> <li>User has many PhotoAlbums</li> <li>PhotoAlbum belongs to User</li> </ul> <p>I achieve something similar using <code>before_create :build_profile</code> to build an empty row for users personal information in my profiles table. This code is inside my user model.</p> <p>I'd like to achieve something similar but in my <code>photo_albums</code> table. On creation of user create an album in the <code>photo_albums</code> table that will store all micropost related images. On creation of user I just need a row with <code>album title = "post album"</code> and their user id.</p> <h3>Model snippet</h3> <pre><code>class User &lt; ActiveRecord::Base has_one :profile, :autosave =&gt; true has_many :photo_albums has_many :microposts before_create :build_profile </code></pre> <h3>Controller's <code>create</code> action</h3> <pre><code>def create @user = User.new(params[:user]) respond_to do |format| if @user.save login @user UserMailer.join_confirmation(@user).deliver format.js { render :js =&gt; "window.location = '#{root_path}'" } else format.js { render :form_errors } end end end </code></pre> <p>How can I do this? Please notice a user has many photo albums so I can't use <code>build_photo_album</code>. I've read some info in the guides but still can't figure this out. Would appreciate a solution.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload