Note that there are some explanatory texts on larger screens.

plurals
  1. POCreate data in other tables when creating user
    primarykey
    data
    text
    <p>Good day.</p> <p>I have 2 tables in my database (PostgeSQL): "Users" and "Folders". Folders are <code>belongs_to :user</code> and user <code>has_many :folders</code>. I need user, when creating his account automatically create one folder in folders table with <code>name: "Default", id: 1, user_id: current_user.id</code>. As I understand correctly, I need to edit <code>create</code> method in <code>users_controller</code></p> <p>This is my <code>user.rb</code></p> <pre><code># == Schema Information # # Table name: users # # id :integer not null, primary key # name :string(255) # email :string(255) # created_at :datetime not null # updated_at :datetime not null # password_digest :string(255) # remember_token :string(255) # admin :boolean default(FALSE) # class User &lt; ActiveRecord::Base attr_accessible :name, :email, :password, :password_confirmation has_secure_password has_many :folders, dependent: :destroy before_save { email.downcase! } before_save :create_remember_token validates :name, presence: true, length: { maximum: 50 } VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false } validates :password, length: { minimum: 6 } validates :password_confirmation, presence: true private def create_remember_token self.remember_token = SecureRandom.urlsafe_base64 end end </code></pre> <p>This is <code>folder.rb</code></p> <pre><code># == Schema Information # # Table name: folders # # id :integer not null, primary key # name :string(255) # user_id :integer # created_at :datetime not null # updated_at :datetime not null # class Folder &lt; ActiveRecord::Base attr_accessible :name belongs_to :user has_many :bookmarks, dependent: :destroy validates :user_id, presence: true validates :name, presence: true end </code></pre> <p>And this is the <code>create</code> method from <code>users_controller.rb</code></p> <pre><code>def create @user = User.new(params[:user]) if @user.save sign_in @user redirect_to @user flash[:success] = "Welcome to Timio Bookmarks!" else if signed_in? redirect_to root_path else render 'new' end end end </code></pre> <p>Thanks for your help!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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