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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:09:52+00:00 2026-05-30T05:09:52+00:00

I am mapping 2 models: User Account class Account has_many :users class User has_one

  • 0

I am mapping 2 models:

User
Account

class Account 
  has_many :users


class User
  has_one :account

The user table as the account_id in it.

Now on the Account model I want to create a ‘primary user’ which an account only has 1 off.
The user table has a boolean flag :is_primary, how can I create a has_one on the account side for a user who has the is_primary and account_id mapped.

So the SQL would look like:

SELECT * FROM users where account_id=123 and is_primary = 1

So I want:

A user has an account.
An account has many users, and has a single primary user also.

  • 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-30T05:09:53+00:00Added an answer on May 30, 2026 at 5:09 am

    Approach 1 – Add a new association

    Add an has_one association with a where lambda. This allows you to work within your current schema.

    class Account 
      has_many :users
      has_one  :primary_user, -> { where(is_primary: true) }, :class_name=> "User"
    end
    

    Now:

    account.users #returns all users associated with the account
    account.primary_user #returns the primary user associated with the account
    # creates a user with is_primary set to true
    account.build_primary_user(name: 'foo bar', email: 'bar@foo.com')
    

    Approach 2 – Add an association method

    class Account 
      has_many :users do
        def primary
          where(:is_primary => true).first
        end
      end
    end
    

    Now:

    account.users.primary # returns the primary account
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a mapping file for the following Model using Fluent
I have a hibernate mapping as follows: <hibernate-mapping> <class name=kochman.elie.data.types.InvoiceTVO table=INVOICE> <id name=id column=ID>
I have 2 models. User: @Entity public class User implements Serializable { Long id;
I have two models like this: class Topic < ActiveRecord::Base has_many :articles end class
i am having a four models.. User,post,tag,comment.. the relationship i want to have with
I'm using Hibernate's JPA impl to model some tables. I'm having trouble mapping a
When you create your mapping files, do you map your properties to fields or
Using NHibernate.Mapping.Attributes, I have a entity class with something like: [Class] public class EntityA
It's a very simple mapping. The database is mysql5.0 User <-one to many-> Blog
I want the current user to read and purchase postings in clubs in which

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.