82397

Mongoid Association Creating (unwanted) Records

Question:

I'm at a loss to why Mongoid is creating a new record in an association. I'm stepping closely through the code, but I've never seen anything like this. I've made a test and slimmed down the code. I left the VCR in just in case it might be related.

it "should not create a duplicate entry for MT" do state = PolcoGroup.create(type: :state, name: 'MT', active: true) s = state.get_senators state.junior_senator = s[:state_junior_senator] # !!!!! this creates a new record state.senior_senator = s[:state_senior_senator] # !!!!! so does this line expect(Legislator.all.size).to eql(2) # actually equals 4 -- each association creates a new record end result is: Legislator.all.map(&:sortname) => ["Tester, Jon (Sen.) [D-MT]", "Walsh, John (Sen.) [D-MT]", "Walsh, John (Sen.) [D-MT]", "Tester, Jon (Sen.) [D-MT]"] ## models class PolcoGroup include Mongoid::Document include Mongoid::Timestamps include VotingMethods include DistrictMethods extend DistrictClassMethods include StateMethods field :name, :type => String ... # STATE RELATIONSHIPS ----------------------------- has_one :junior_senator, class_name: "Legislator", inverse_of: :jr_legislator_state has_one :senior_senator, class_name: "Legislator", inverse_of: :sr_legislator_state ... end class Legislator include Mongoid::Document include Mongoid::Timestamps # the following fields are directly from govtrack field :govtrack_id, type: Integer field :bioguideid, type: String ... belongs_to :jr_legislator_state, class_name: "PolcoGroup", inverse_of: :junior_senator belongs_to :sr_legislator_state, class_name: "PolcoGroup", inverse_of: :senior_senator ... end module StateMethods def get_senators ... # just returns the following {state_senior_senator: senators.first, state_junior_senator: senators.last} end end

You can see more code here: <a href="https://gist.github.com/tbbooher/d892f5c234053990da70" rel="nofollow">https://gist.github.com/tbbooher/d892f5c234053990da70</a>

Answer1:

OK -- never do what I did. I was pulling in an old version of mongo as a test database and then conducting the above. Of course it wasn't working correctly.

Recommend

  • The requested resource is not available.on running spring mvc program on eclipse with tomcat
  • Second Y-Axis in a R plotly graph
  • SQL - Pivot multiple columns without Aggregrates
  • Magento mass reindexing gives internal server error
  • a stable way to check domain availability with pywhois
  • Python dateutil parser fails
  • HTTP GET to amazon aws from jquery or XMLHttpRequest fails with Origin is not allowed by Access-Cont
  • Dokuwiki with LDAP error: User authentication is temporarily unavailable
  • Create a multiple horizontal line plot, plotting numerous variable for multiple years
  • How configure delete-selection-mode to only delete?
  • Oracle 10g: Inserting multiple rows
  • Python redis subscribe can not get all datas?
  • Secure Webservice (NTLM) - Jmeter
  • SQL: Error when converting varchar to datetime
  • Is sparse matrix-vector multiplication faster in Matlab than in Python?
  • SignTool error: Access is denied
  • Highcharts: display multiple tooltips by click and vice versa
  • Run PHPUnit by terminal with Docker
  • Converting string timestamp to date [duplicate]
  • Moment.js only within the last week
  • Date and time regular expression
  • Cumulative sum of values by month, filling in for missing months
  • Is it possible to plot two variables using a third one as filter in R?
  • How to configure setenv.sh for tomcat8?
  • Catching UnknownHostException of RetrievalUrl in WorldWindJava 2.1 in Netbeans
  • php is_file always return false
  • In-place sed command not working
  • Eloquent paginate function in Slim 3 project using twig
  • FB SDK and cURL: Unknown SSL protocol error in connection to graph.facebook.com:443
  • Using $this when not in object context
  • How do I fake an specific browser client when using Java's Net library?
  • How reduce the height of an mschart by breaking up the y-axis
  • Perl system calls when running as another user using sudo
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Bitwise OR returns boolean when one of operands is nil
  • sending mail using smtp is too slow
  • MATLAB: Piecewise function in curve fitting toolbox using fittype
  • Busy indicator not showing up in wpf window [duplicate]
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?