Ruby on Rails ActiveRecord :: AssociationTypeMismatch

I am trying to create a private messaging system for my website, and I am currently working on message replies. However, I ran into an ActiveRecord :: AssociationTypeMismatch problem. This error message is:

ActiveRecord :: AssociationTypeMismatch in RepliesController # create

Message (# 58297820) expected to get a String (# 1635350)

I tried to find out what the problem is now, with no luck.

Below you will find my code for my migration, model, view and controller.

Migration

def self.up create_table :replies do |t| t.integer :message_id, :null => false t.integer :sender_id, :null => false t.text :message, :null => false t.timestamps end end 

Model

 class Reply < ActiveRecord::Base belongs_to :message validates_presence_of :message cattr_reader :per_page @@per_page = 10 end 

View

 <% form_for(@reply) do |f| %> <table> <tr> <td><%= f.text_area :message %></td> </tr> <%= f.hidden_field :message_id, :value => @message.id %> <tr> <td><%= f.submit 'Reply', :id => 'replySubmit' %></td> </tr> </table> <% end %> 

Controller

 def create account = Account.getAccountById(session[:user]) message = Message.find( params[:reply][:message_id], :conditions => ["messages.account_id=? or messages.sender_id=?", account.id, account.id] ) if message @reply = Reply.new @reply.message_id = message.id @reply.sender_id = account.id @reply.message = params[:reply][:message] if @reply.save flash[:message] = "Reply successfully submitted." redirect_to(messages_path) else flash[:warning] = "Message cannot be blank." redirect_to(messages_path) end else redirect_to(messages_path) end rescue ActiveRecord::RecordNotFound render :template => "error" end 

I would be grateful for the help provided. I will try to find out what the problem is.

Thanks.

Update: Stacktrace

 RAILS_ROOT: C:/Users/redbush/Desktop/biomixr Application Trace | Framework Trace | Full Trace C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record /associations/association_proxy.rb:259:in `raise_on_type_mismatch' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/associations/belongs_to_association.rb:22:in `replace' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/associations.rb:1287:in `message=' C:/Users/redbush/Desktop/biomixr/app/controllers/replies_controller.rb:14:in `create' 

Request

Options:

 {"commit"=>"Reply", "reply"=>{"message"=>"sssss", "message_id"=>"4"}, "authenticity_token"=>"SMVfiolNAVPmLLU0eOWzx2jPFbujMtpyqQcs6A2Mxr0="} 

Show Session Dump Response

Headers:

 {"Content-Type"=>"", "Cache-Control"=>"no-cache"} 
+4
source share
3 answers

Your problem is that you have a relationship with message , and you also want to use the text box in the same model called message . When you create a relationship, you have access to some new methods that will step on the feet of other getters and setters.

To fix this change, textbox name to another name and reflect the changes in your controller.

If you need more information, let me know.

+5
source

The belongs_to relationship creates a message attribute in your model that expects an object of type Message. However, you also have a message attribute of type string. You can get around this using:

 belongs_to :my_message, :class_name => :message 

And the corresponding message will be available as my_message , while the text field will be available as message . In general, it seems that you are trying to handle too much work with attitude yourself - let the rails do your work.

+1
source

You have a conflict in the design of your model. You have both an association and an attribute with a name.

The error you get is that Rails expects you to populate an association instead of an attribute.

I suggest changing my attribute from message to message_text, for example:

 @reply = Reply.new @reply.message_id = message.id @reply.sender_id = account.id @reply.message_text = params[:reply][:message] 

Make changes to the migration file and run it.

0
source

All Articles