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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T18:06:01+00:00 2026-05-12T18:06:01+00:00

A user has many employments. What do you think? Is this a valid and

  • 0

A user has many employments.

What do you think?
Is this a valid and clear way to fetch all siblings (belonging to the same user) of a given employment object?

class Employment < ActiveRecord::Base
  belongs_to :user

  has_many :silblings,
    :primary_key => :user_id,
    :foreign_key => :user_id,
    :class_name => 'Employment'
end

This can be extended with the following named scope:

  named_scope :except, lambda {|id| {:conditions => ["id != ?", id]} if id}

Now I can do stuff like:

  self.silblings.except(self.id).each do |silbling|
    puts silbling
  end

The resulting SQL statement looks like:

  SELECT * FROM `employments` 
  WHERE (`employments`.user_id = 49) 
  AND ((id != 46) AND (`employments`.user_id = 49))

Comments like ‘no, you abuse XY, rather use this XZ’ are very welcome!

Reto

  • 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-12T18:06:01+00:00Added an answer on May 12, 2026 at 6:06 pm

    Looks fine. Except that the SQL doubles ('employments'.user_id = 49) in the query. Which is nothing major. If it’s something you really don’t want, you could go about defining siblings like this:

    class Employment < ActiveRecord::Base
      belongs_to :user
    
      named_scope :for_user, lambda { |user|
        { :conditions => {:user_id => user} }
      }
    
      named_scope :except, lambda {|employment|
        {:conditions => ["id != ?", employment}
      }
    
      def siblings 
        Employment.for_user(user_id).except(id)
      end
    
    end
    

    Believe it or not you can still call named scopes on @employment.siblings. Although doing things this way means you can’t assign to siblings. The siblings call comes out a little cleaner. There may be a performance improvement, but it probably won’t be significant to make a difference.

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

Sidebar

Related Questions

Given a parent child relationship between User and FailedLogin where a user has many
why cake PHP framework not recognize true syntax in this example User has Many
given the fact that a user has many credit cards and a credit card
I have a many-to-many relationship like this: A user has_many organizations through affiliations and
Let's say I have a schema where User has many Alerts (many to many).
I have a User model and an Account model. The user has many accounts
Each user has many roles; to find out whether a user has the admin
I have three models: User Award Trophy The associations are: User has many awards
A user has many user_levels and a user_level belongs to a user. Here is
Each user HAS MANY photos and HAS MANY comments. I would like to order

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.