Strange ActiveRecord::AssociationTypeMismatch
This is an error that occurs when two different versions of the model have been loaded. I used to hit it in an older version of Rails 3, since the development environment's model reloader was slightly glitched. The numbers after the class name refer to different versions of the class.
It stands to reason that this sort of error might come up in development mode, but it shouldn't in test mode, because, by default, classes are cached. See the config/environments/test.rb
file to ensure that cache_classes
is set to true.
Also check that you're on the latest version of Rails, 3.0.7. This may be a bug that has since been fixed. While we're at it, check that you're on factory_girl 1.3.3. When using the API totally correctly, which I think you're doing, the only possibilities left are that something is misconfigured or that it's a bug in someone else's code.
Rails 4 ActiveRecord::AssociationTypeMismatch
You're trying to add UserAction
object to UserLog
collection which will definitely throw a TypeMismatch
error. Try this instead:
user = User.first
action = UserAction.first
user.user_actions << action
user.save
Rails: ActiveRecord::AssociationTypeMismatch error
The issue is obvious from the answers above - change user_name
attribute to be called something else.
As a very last resort, this might be a bad suggestion, but you could even try to declare which attribute you're using like:
class Blog < ApplicationRecord
belongs_to :user_name, through: :user_name_id
end
But again, the reason you're associating the records, is so that you can call user_name at any point, through the association, and get all the information stored there. Meaning, you don't need to store the user_name with the blog... because you already are through the user_name association.
Ruby on Rails ActiveRecord::AssociationTypeMismatch
Your problem is that you have a relation to message
and you also want to use a text field on the same model called message
. When you create a relation you have access to some new methods that will step on the feet of other getter and setters.
To fix this change the name of the text field to another name and reflect the changes in your controller.
If you need more detail let me know.
ActiveRecord::AssociationTypeMismatch but input is correct
With this right here:
Notification.create!(recipient: @room.reciever_id, actor_id: current_user
you are basically doing it backward - you're passing an id as the recipient
and a record as the actor_id
.
Remember that if you're using the _id
field you should be passing an id, and if you're using the association name (e.g. recipient
) you should pass the actual record:
following should work:
Notification.create!(recipient_id: @room.reciever_id, actor_id: current_user.id
If you had an association receiver
on Room
, you could say:
Notification.create!(recipient: @room.reciever, actor: current_user
however this is slightly worse performance because it does 1 more database lookup (querying for the whole @room.receiver
object instead of reading the receiver_id
directly off @room
)
Ruby/Rails: AssociationTypeMismatch with the correct association, but it expects different object id? Whaaa?
So the answer, as Frederick Cheung pointed out was to set cache_classes
to true
in test.rb
.
This supposedly conflicts with spork, which is why I was told to turn it off in the first place--since you want spork to re-load your models and whatnot whenever you run another test. The solution is to set cache_classes
to true
but also put ActiveSupport::Dependencies.clear
in your spork prefork block.
References:
- ActiveSupport:Dependencies
- Reloading Models
- Spork tips
- Spork cache_classes explanation
ActiveRecord::AssociationTypeMismatch gotNilClass
You have nil
object in params[:estados]
, and Rails can't save this association.
Easiest way to remove them is to call params[:estados].compact!
after line 14
Related Topics
Ruby, Check If Date Is a Weekend
How to Replace Multiple Newlines in a Row with One Newline Using Ruby
What Is the Preferred Way (Better Style) to Name a Namespace in Ruby? Singular or Plural
Rails: Render Doesn't Work, Still Get 'Template Is Missing'
What Is the Second Parameter/Argument to CSV.Open( ) in Ruby
Sort an Array of Strings by Their Integer Values
Gem::Loaderror: Specified 'Sqlite3' for Database Adapter, But the Gem Is Not Loaded
How to Do Sane "Set-Difference" in Ruby
Why Does Foreman Not Output Some Things Until I Press Control-C
How to Configure an Extra/Different Migrations Folder
Gem Install Nokogiri -V '1.6.8.1' Fails
$ Bundle Exec Rake Db:Reset Command Raising Couldn't Drop Db/Development.Sqlite3
Are There Unintended Consequences of Ruby's 'Begin ... End' Without 'Rescue' Used as a Code Block
How to Download a CSV File in Ruby on Rails
Why and When to Use the Shell Instead of Ruby
MAC Os X Mountain Lion "Rails Is Not Currently Installed on This System."