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

  • Home
  • SEARCH
  • 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 925051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:27:32+00:00 2026-05-15T19:27:32+00:00

This is a Ruby code: if (@user.isAdmin?) @admin_profile = AdminProfile.new @user.admin_profile = @admin_profile @admin_profile.save

  • 0

This is a Ruby code:

  if (@user.isAdmin?)
    @admin_profile         = AdminProfile.new 
    @user.admin_profile    = @admin_profile 
    @admin_profile.save
    @user.admin_profile_id = @admin_profile.id 
  else        
    @personal_profile = PersonalProfile.new
    @user.personal_profile = @personal_profile
    @personal_profile.save
    @user.personal_profile_id = @personal_profile.id
  end                

Is it possible to DRY this code? Two code is very similar, but as you can see, they have some difference, is it possible to make it simpler?

  • 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-05-15T19:27:33+00:00Added an answer on May 15, 2026 at 7:27 pm

    As a first step you could use the same variable regardless of the profile type i.e.

    @profile = @user.isAdmin? ? AdminProfile.new : PersonalProfile.new
    

    This is using Ruby’s conditional operator which has the form condition ? value if true : value if false. i.e. if @user.isAdmin? evaluates to true then @profile gets the value after the ?. If @user.isAdmin? is false then @profile gets the value after the :. Notice that because your method name already ends in a ? you get this appearance of a double ?.

    and then

    if (@user.isAdmin?)
      @user.admin_profile = @profile 
      @user.admin_profile_id = @profile.id 
    else        
      @user.personal_profile = @profile
      @user.personal_profile_id = @profile.id
    end 
    

    Also, not sure if this is Rails code, but if it is then you don’t need to set admin_profile and admin_profile_id, and in fact @profile.id won’t be set yet as the profile hasn’t saved. So possibly you could reduce the if/else to:

    if (@user.isAdmin?)
      @user.admin_profile = @profile 
    else        
      @user.personal_profile = @profile
    end 
    

    Update

    You should also look into the create_association method which you get when you use a belongs_to association. You can have Rails create and save an associated object all in a single step e.g.

    @user.create_personal_profile
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 537k
  • Answers 537k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer 1) DATEADD() with MONTH of 1 to 2010-01-31 will result… May 17, 2026 at 1:46 am
  • Editorial Team
    Editorial Team added an answer I believe that you can't involve the "z-index" into the… May 17, 2026 at 1:46 am
  • Editorial Team
    Editorial Team added an answer The regular expression \\ matches a single backslash. In JavaScript,… May 17, 2026 at 1:46 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

This is the code I have at the end a the create method in
Does anyone have any insights regarding compiling Ruby code for Windows? I've tried both
Having looked at this question, I have the following code: $/ = \0 answer
I have the following code. I'm still a newbie in Ruby on Rails. As
I want to create a alias method in ruby and pass parameters to this.
Using Ruby on Rails combined with capistrano and git, I've run into an annoying
This is probably a stupid question but I'll go ahead and humble myself. The
I realize this sounds a little crazy, but I'm working on a project for
I have the following expression: ^\w(\s(+|-|/|*)\s\w)*$ This simply looks to match a mathematical expression,
I have built a blog application w/ ruby on rails and I am trying

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.