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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:40:05+00:00 2026-05-23T09:40:05+00:00

I have the following model: class Bill . . . some fields . .

  • 0

I have the following model:

class Bill
 . . . some fields . . .
belongs_to :sponsor, :class_name => "Legislator"
end
class Legislator
  .. .some fields . . .
  has_many :bills
end

I get this strange behavior, but I am sure this is something simple:

Loading development environment (Rails 3.0.7)
b = Bill.first
l = Legislator.first
l.bills << b
l.save
=> true
(I can view l.bills, but l.bills.all.to_a.count is 0)
 l.govtrack_id
=> 400001
ruby-1.9.2-p180 :007 > Legislator.where(govtrack_id: 400001).first.bills
 => [] 

So I can create the association and view it. The save is successful, but when I retrieve the object, the association is gone . . . no errors. I’m confused, what am I missing?

  • 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-23T09:40:06+00:00Added an answer on May 23, 2026 at 9:40 am

    You’re missing inverse_of on your Legislator model. I ran a quick test (to make sure there wasn’t a Mongoid issue). My models were thus:

    class Bill
      include Mongoid::Document
      include Mongoid::Timestamps
      field :name
      belongs_to :sponsor, :class_name => "Legislator"
    end
    
    class Legislator
      include Mongoid::Document
      include Mongoid::Timestamps
    
      field :govtrack_id
      has_many :bills, :inverse_of => :sponsor
    end
    

    And console output from the test:

    ruby-1.9.2-p180 > Bill.create(:name => "A new bill")
      => #<Bill _id: 4e0822636a4f1d11c1000001, _type: nil, created_at: 2011-06-27 06:25:39 UTC, updated_at: 2011-06-27 06:25:39 UTC, name: "A new bill", sponsor_id: nil>
    ruby-1.9.2-p180 > Legislator.create(:govtrack_id => "400123")
      => #<Legislator _id: 4e0822786a4f1d11c1000002, _type: nil, created_at: 2011-06-27 06:26:00 UTC, updated_at: 2011-06-27 06:26:00 UTC, govtrack_id: "400123">
    ruby-1.9.2-p180 > l = Legislator.first
    ruby-1.9.2-p180 > l.bills << Bill.first
      => [#<Bill _id: 4e0822636a4f1d11c1000001, _type: nil, created_at: 2011-06-27 06:25:39 UTC, updated_at: 2011-06-27 06:26:08 UTC, name: "A new bill", sponsor_id: BSON::ObjectId('4e0822786a4f1d11c1000002')>] 
    ruby-1.9.2-p180 > l.save!
      => true
    ruby-1.9.2-p180 > Bill.first.sponsor.govtrack_id
      => "400123"
    ruby-1.9.2-p180 > Legislator.first.bills
     => [#<Bill _id: 4e0822636a4f1d11c1000001, _type: nil, created_at: 2011-06-27 06:25:39 UTC, updated_at: 2011-06-27 06:26:08 UTC, name: "A new bill", sponsor_id: BSON::ObjectId('4e0822786a4f1d11c1000002')>]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a django application I have the following model: class Appointment(models.Model): #some other fields
I have the following model: class Advisor < ActiveRecord::Base belongs_to :course end class Course
I'm having a problem testing the following model: class Bill < ActiveRecord::Base belongs_to :consignee
I have the following model class Plugin(models.Model): name = models.CharField(max_length=50) # more fields which
I have the following model: public class Product { public int Id { get;
I have the following model: class item < ActiveRecord::Base has_one :price end class price
I have following model: class Customer < ActiveRecord::Base has_one :cart end class Cart <
I have the following model: class UptimeManager(models.Manager): def with_length(self): Get querySet of uptimes sorted
I have the following models: class Bill(models.Model): date = models.DateTimeField(_(Date of bill),null=True,blank=True) class Item(models.Model):
I have the following model : class Contract { string ContractID{get;set;} ICollection<Part> Parts{get;set;} }

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.