Strange Activerecord::Associationtypemismatch

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:

  1. ActiveSupport:Dependencies
  2. Reloading Models
  3. Spork tips
  4. 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



Leave a reply



Submit