user11127396
user11127396

Reputation:

How do I seed data of a class which requires the user_id of the current user?

I am making a fitness web application as part of a project. This project has 5 models. User, Muscle_Groups, Diet, Workout and Meal. The associations are on the code below. As of now, I have a new page and a show page for the User. I want to redirect the user from the show page to the muscle_group index page where it will list all the muscles in a persons body. The User obviously has many Muscle_Groups, I want to seed the muscle_group index page with all muscle groups (biceps, back, legs, chest). The issue is, I need to create these instances with the user_id of the current user using the app and I have no idea how to do it at this point. I hope my brief explanation helps, my code is below.


class User < ApplicationRecord
    has_many :diets
    has_many :muscle_groups


    before_save { self.email = email.downcase }
    #before saving, the email is lowercased
    validates :name, presence: true, length: { maximum: 50 }
    #validates the names presence, max char length is 50 

    validates :weight, presence: true, numericality: true
    validates :height, presence: true, numericality: true

    validates_inclusion_of :gender, :in => %w( m f male Male female Female)
    validates :age, presence: true, numericality: {only_integer: true }

    VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
    validates :email, presence: true, length: { maximum: 255 },
                      format: { with: VALID_EMAIL_REGEX },
                      uniqueness: { case_sensitive: false }

    has_secure_password
end

class Muscle_Group < ApplicationRecord
    has_many :workouts
    belongs_to :user
end

class Diet < ApplicationRecord
    belongs_to :user 
    has_many :meals
end

class Meal < ApplicationRecord
    belongs_to :diet
end

class Workout < ApplicationRecord
    belongs_to :muscle_group
end

class UsersController < ApplicationController

    def new #render's the sign up page for a new user
        @user = User.new 
    end 

    def create #action to create the user
        @user = User.create(user_params)

        if @user.save 
            log_in @user 
            flash[:success] = "Are you ready to GitFit?!"
            redirect_to @user #redirects to the user's show page, which is the main menu
        else 
            render 'new'
        end 
    end 

    def show 
        @user = User.find(params[:id])
    end

    private 
        def user_params 
            params.require(:user).permit(:name, 
            :email, 
            :weight,
            :height, 
            :gender,
            :age,
            :password, 
            :password_confirmation)
        end 


end

class Muscle_GroupsController < ApplicationController 

    def index
        @muscle_groups = Muscle_Group.all
    end 
end 

Upvotes: 1

Views: 246

Answers (1)

Clara
Clara

Reputation: 2715

Just create one sample user in the seed file and associate the muscle groups to him. Then login with this account and you will have the results. For instance like this

# seed.rb

sample_user = User.create(name: "Joe", weight: 100, height: 180, 
                          age: 23, email: "[email protected]", 
                          password: "123456")

MuscleGroup.create(user: sample_user, ...)
Diet.create(user: sample_user)
...

I don't know the exact fields and the measuring system you use, but it could look like that.

Another way in production would be to sign up as a user of the website. And then find yourself in the console (rails c) and add the muscle_group and diet and so on and connect it manually to your user.

Upvotes: 2

Related Questions