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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:16:15+00:00 2026-06-16T22:16:15+00:00

I use Rails3.2.8 to do some practice, here is my models: class Incident <

  • 0

I use Rails3.2.8 to do some practice, here is my models:

class Incident < ActiveRecord::Base
  attr_accessible :category, :user, :status, :reference, :location
  belongs_to :user
  has_one :location
  accepts_nested_attributes_for :location

  validates_presence_of :location, :user, :category


end

class Location < ActiveRecord::Base
  attr_accessible :latitude, :longitude, :street
  belongs_to :incident
end

Here is my test:

require 'spec_helper'

describe Incident do
  before (:each) do
    @user = create(:user, :name => "user1")
    @incident_data = {:category => "House Break in", :user => @user,
                      :location => {:latitude => "-28.1940509", :longitude => "28.0359692",
                                    :street => "abc name"}}
  end
  describe "After create Incident successfully" do
    it "should create location" do
      incident = Incident.create(@incident_data)

      expect(incident.location.latitude).to eq("-28.1940509")
    end
  end
end

What I want to do is to create Location object automatically when creating Incident object. But the test failed by the following reason:

Failure/Error: incident = Incident.new(@incident_data)

ActiveRecord::AssociationTypeMismatch:

Location(#70156311891820) expected, got Hash(#70156307112200)

Any ideas?

  • 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-06-16T22:16:16+00:00Added an answer on June 16, 2026 at 10:16 pm

    It explicitly says that location should be an instance of Location, not a Hash. You have

    :location => {:latitude => "-28.1940509", :longitude => "28.0359692",
                                    :street => "abc name"}
    

    But as soon you are using nested attributes, it should be location_attributes (see NestedAttributes docs):

    :location_attributes => {:latitude => "-28.1940509", :longitude => "28.0359692",
                                    :street => "abc name"}
    

    or you can just create Location object

    :location => Location.new(:latitude => "-28.1940509", :longitude => "28.0359692",
                                    :street => "abc name")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use some rails code withing a javascript and need to
So when you use scaffolding Rails will kick out some layouts that are xhtml
I'm using rails_admin which use devise. Some words still displays in english. I have
I encounter identical issues with some Ruby gems in Rails application. When I use:
So I am trying to use the rails3 branch of Nifty Generators by Ryan
After installing rails3, I'm experiencing problems when trying to use haml with it. I
I typically use rails models and typus as my CMS as it gives the
Looking for some general advice and tips about using cancan on our latest rails3
I want to develop a digg-like website and decide to use Rails3. I am
Deploying a Rails3 app, and am having some issues getting rake to find the

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.