ruby - rails 4-- User expected, got string -
ruby - rails 4-- User expected, got string -
i have simple messaging scheme built rails app.
the table looks this:
create_table :messages |t| t.integer :from_id t.integer :to_id t.string :subject t.text :message t.timestamps end
the model simple well:
belongs_to :to_id, class_name: "user", :foreign_key => "to_id" belongs_to :from_id, class_name: "user", :foreign_key => "from_id"
when submit message form however, getting type mismatch
, , when seek create new message in rails console, throws same error.
user(#-588576898) expected, got string(#-603326958)
apparently expecting user
object rather user.id
value. assume due error in how created model?
where did go wrong?
edit:
the form code below. however, note experiencing same issue when creating message
straight rails console, assuming it's model:
> m = message.new => #<message id: nil, from_id: nil, to_id: nil, subject: nil, message: nil, created_at: nil, updated_at: nil> > m.from_id = 1 activerecord::associationtypemismatch: user(#-582821898) expected, got fixnum(#-597136528)
here's form, stripping labels , html:
<%= form_for @message, :url => {:controller => "messages", :action => "create"}, :html => {:class => 'form-horizontal form', :role => 'form'} |f| %> <%= f.select :to_id, options_from_collection_for_select(user.all, :id, :first_name) %> <%= f.text_field :subject %> <%= f.text_area :message %> <%= f.submit class: "btn btn-large btn-primary" %> <% end %>
the name of association (the first argument belongs_to
) should not same name of column contains foreign key.
for example
belongs_to :to, class_name: "message"
as stands accessor methods generated association shadowing accessor method attribute.
ruby-on-rails ruby ruby-on-rails-4
Comments
Post a Comment