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 8865965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:39:44+00:00 2026-06-14T16:39:44+00:00

I have these two models user : class User < ActiveRecord::Base attr_accessible :name, :provider,

  • 0

I have these two models user:

class User < ActiveRecord::Base
 attr_accessible :name, :provider, :uid

 # This is a class method, callable from SessionsController 
 # hence the "User." 
 def User.create_with_omniauth( auth)
   user = User.new() 
   user.provider = auth["provider"] 
   user.uid = auth["uid"] 
   user.name = auth["info"]["name"] 
   user.save 
   return user     
  end

 has_one :userprofile
end

and userprofile:

class Userprofile < ActiveRecord::Base
  belongs_to :user
  attr_accessible :age, :fname, :gender, :lname, :photo_url
end

I’d like to check if there’s a userprofile object associated to the user. If there’s one, display it. Otherwise, create a new one.

I’m trying this and getting an error.

def show
 @userprofile = current_user.userprofiles.all.where(:user_id => current_user.uid)
 if !@userprofile.nil? then
   @userprofile
 else
   @userprofile = Userprofile.new
 end
end

undefined method `userprofiles’ for #

I’ve tried find with no better result.

  • 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-14T16:39:46+00:00Added an answer on June 14, 2026 at 4:39 pm

    user and userprofile have one-to-one relationship so that

    @userprofile = current_user.userprofile
    

    by using this you can get userprofile of current_user

    now your show method look like

    def show
     if current_user.userprofile.present?
       @userprofile = current_user.userprofile
     else
      @userprofile = current_user.build_userprofile
     end
    end
    

    Update : why build

    http://edgeguides.rubyonrails.org/association_basics.html#has-one-association-reference

    we use build_userprofile because it’s one-to-one relation. but suppose if it’s has_many relationship then we use userprofiles_build

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

Sidebar

Related Questions

so I have two models: class User < ActiveRecord::Base has_and_belongs_to_many :followed_courses, :class_name => Course
I have two models class User < ActiveRecord::Base has_many :posts searchable do text :post_titles
Let's assume I have two models. class User < ActiveRecord::Base has_one :blog end class
I have the following two models: class Product < ActiveRecord::Base belongs_to :shop validates_numericality_of :price,
I have the following two models: class Message < ActiveRecord::Base belongs_to :to_user, :class_name =>
I have the following two models: class PhotoAlbum < ActiveRecord::Base belongs_to :project has_many :photos,
On my server, I have two models: Broadcast class Broadcast < ActiveRecord::Base validates_presence_of :content
I have two models: countries and users country.rb class Country < ActiveRecord::Base has_many :users
i have these two models public class Group { [Key] public int GroupID {
I have these two methods on a class that differ only in one method

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.