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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T16:28:15+00:00 2026-05-21T16:28:15+00:00

This seems like a bug in Rails to me, but there’s probably not much

  • 0

This seems like a bug in Rails to me, but there’s probably not much I can do about that. So how can I accomplish my expected behavior?

Suppose we have:

class User < ActiveRecord::Base
  has_many :awesome_friends, :class_name => "Friend", :conditions => {:awesome => true}
end

And execute the code:

>> my_user.awesome_friends << Friend.new(:name=>'jim')

Afterwards, when I inspect this friend object, I see that the user_id field is populated. But I would also expect to see the “awesome” column set to ‘true’, which it is not.

Furthermore, if I execute the following from the console:

>> my_user.awesome_friends << Friend.new(:name=>'jim')
>> my_user.awesome_friends
= [#<Friend id:1, name:"jim", awesome:nil>]
# Quit and restart the console
>> my_user.awesome_friends
= []

Any thoughts on this? I suppose the conditions hash could be arbitrarily complex, making integration into the setter impossible. But in a way it feels like by default we are passing the condition “:user_id => self.id”, and that gets set, so shouldn’t others?

Thanks,
Mike

EDIT:

I found that there are callbacks for has_many, so I think I might define the relationship like this:

has_many :awesome_friends,
         :class_name => "Friend",
         :conditions => {:awesome => true},
         :before_add => Proc.new{|p,c| c.awesome = true},
         :before_remove => Proc.new{|p,c| c.awesome = false}

Although, it’s starting to feel like maybe I’m just implementing some other, existing design pattern. Maybe I should subclass AwesomeFriend < Friend? Ultimately I need a couple of these has_many relationships, and subclassing get’s messy with all the extra files..

EDIT 2:

Okay, thanks to everyone who commented! I ultimately wrapped up the method above into a nice little ActiveRecord extension, ‘has_many_of_type’. Which works like follows:

has_many_of_type :awesome_friends, :class_name => "Friend", :type=>:awesome

Which just translates to has_many with the appropriate conditions, before_add, and before_remove params (and it assumes the existence of a column named friend_type).

  • 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-21T16:28:16+00:00Added an answer on May 21, 2026 at 4:28 pm

    You need use:

    my_user.awesome_friends.create(:name=>'jim') or my_user.awesome_friends.build(:name=>'jim')

    In documentation:
    has_many (:conditions)

    Record creations from the association are scoped if a hash is used. has_many :posts, :conditions => {:published => true} will create published posts with @blog.posts.create or @blog.posts.build.

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

Sidebar

Related Questions

This really seems like a bug to me, but perhaps some databinding gurus can
This seems like a bug to me... I accept that automatic properties, defined as
Can anyone recreate this issue? It seems to me like a quite serious bug
This seems like a repeated question but i'm not able to get my answer.
This seems like the simplest Git question, but I can't find ANYTHING on it.
This seems like something that could be quite useful and yet and I can't
This seems like such a simple issue but I cannot find an elegant solution.
This seems like a must have form input for a mobile ui framework, but
This seems like another fairly simple thing to do, but I'm again struggling on
This seems like a serious bug : private void LayoutRoot_Drop(object sender, DragEventArgs e) {

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.