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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:56:48+00:00 2026-05-20T10:56:48+00:00

I’ve got a simple user: **Edited a bit for clarity, and on Sam’s suggestion

  • 0

I’ve got a simple user: **Edited a bit for clarity, and on Sam’s suggestion

class User < ActiveRecord::Base
  # <snip other attribs>
  has_many :handles
  has_one :active_handle, :class_name => "Handle"

  validates_each :active_handle, :allow_nil => true  do |record, attr, value|
    record.errors.add attr, "is not owned by the correct user" unless record.handles.include?(value)
  end
end

And a handle model:

class Handle < ActiveRecord::Base
  belongs_to :user
  attr_accessible :user_id
  validates :user_id,
        :presence => true,
        :numericality => true

  attr_accessible :name
  validates :name,
        #etc...
end

Now I’d like to check, when setting the User.active_handle association, that the handle is owned by the correct Handle.user_id. I’ve tried to do this in a custom validation, and also in a validate method on the User model. Both ways, it does the exact opposite of what I want, it sets the user_id of handle to the User doing the checking.

I’m at the end of my rope, clearly I don’t understand something, and google isn’t getting me anywhere I haven’t already been.

ETA: I have also tried to manipulate the has_one association with conditions, that seems to fail too…

has_one :active_handle,
        :class_name => "Handle",
        :conditions => ['user_id =?', '#{self.id}']
  • 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-20T10:56:48+00:00Added an answer on May 20, 2026 at 10:56 am

    I’d take a different approach to this problem; the double link is messy, and hard to maintain, as you’ve discovered. Instead, I’d put an active flag in the handle model, and order the :active_handle association on that flag:

    class User < ActiveRecord::Base
        has_many :handles
        has_one  :active_handle, :class_name => 'Handle', :order => 'handles.active DESC'
    end
    

    Now there’s only one link, and it’s the same link that’s already in place to establish the plain has_many :handles association (namely, the user_id attribute in the Handle model). Finding the active handle in now just a matter of finding the User’s first Handle with the active flag set. And I’d use :order rather that :conditions, as that’ll give you a nice fallback: If no Handle for that user has the active flag set, Rails’ll pick one to use as a default.

    And then, in your handle model, you can have a fairly simple activate function:

    class Handle < ActiveRecord::Base
        # ...stuff...
    
        def activate!
            # Pseudocode - sorry!
            sql('UPDATE handles SET active = 0 WHERE user_id = ?', self.user_id)
            self.active = true
            self.save!
        end
    end
    

    Or, if you wanted to be able to call something like user.handles.activate(hdl), you could put something similar as an association extension. Or user.active_handle = hdl. Or…

    Hope this helps!

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

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
i got an object with contents of html markup in it, for example: string
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm making a simple page using Google Maps API 3. My first. One marker

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.