开发者

Ruby on Rails ActiveRecord::AssociationTypeMismatch

I'm trying to create a private messaging system for my website, and I am currently working on message replies. I've run into an ActiveRecord::AssociationTypeMismatch problem however. This is the error message:

ActiveRecord::AssociationTypeMismatch in RepliesController#create

Message(#58297820) expected, got String(#1635350)

I've been trying to figure out what the problem is for awhile 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&开发者_运维百科gt;
                    </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'd appreciate any help provided. I'll keep trying to figure out what the problem is.

Thank you.

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

Parameters:

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

Show session dump Response

Headers:

{"Content-Type"=>"",
"Cache-Control"=>"no-cache"}


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.


The belongs_to relation creates a message attribute on your model, which expects an object of type Message. However, you also have a message attribute of type string. You can workaround this by using:

belongs_to :my_message, :class_name => :message

And the related message will be available as my_message, while the text field will be available as message. In general, it seems like you're trying to handle too much of the relation work on your own - let rails do the work for you.


You have a clash in the design of your model. You have both an association and an attribute named message.

The error you are getting is because Rails is expecting you to fill up the association instead of the attribute.

I suggest changing your attribute from message to message_text, like so:

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

Do make the changes in your migration file and run it too.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜