ActiveRecord::AssociationTypeMismatch

I have the requirement to empty the association records and add the
association records as new (as below).

@task = Task.find(params[:task_id])

@task.task_users = []

params[:ids].split(", ").each do |court_user_id|
task_user = TaskUser.new(:court_user_id => court_user_id)
@task.task_users << task_user
end

@task.save

This is not working. Following error is thrown.

ActiveRecord::AssociationTypeMismatch (TaskUser expected, got TaskUser):
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.6/lib/active_record/associations/association_proxy.rb:148:in
raise_on_type_mismatch' C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.6/lib/active_record/associations/association_collection.rb:24:in<<’
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.6/lib/active_record/associations/association_collection.rb:23:in
each' C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.6/lib/active_record/associations/association_collection.rb:23:in<<’
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.6/lib/active_record/connection_adapters/abstract/database_statements.rb:59:in
`transaction’

Any body know why this error has thrown for the above logic.

Task.rb has the following code.

belongs_to :court
has_many :workflow_tasks
has_many :task_users, :dependent => :destroy
has_many :court_users, :through => :task_users

I would appreciate if anyone can address this issue.

Thanks,
Ayyanar. A

At present, iam using the below logic to make the application work, but
i don’t like this logic since you know the loopholes in the logic. No
transaction.
I want the above logic to work.

@task = Task.find(params[:task_id])

TaskUser.delete_all(“task_id=#{params[:task_id]}”)

params[:ids].split(", ").each do |court_user_id|
task_user = TaskUser.new(:court_user_id => court_user_id, :task_id =>
params[:task_id])
task_user.save
end

This is the same problem as your undefined method: the autoloading is
getting screwed, probably because of you requiring a class by hand
that you didn’t need to. This results in there being more than one
instance of the TaskUser class hanging around: the one from before
rails reset your application following the previous request and the
one from the new request.

Fred

ActiveRecord::AssociationTypeMismatch in ClistingsController#create

Category(#-612181558) expected, got String(#-608036348)

Rails.root: /home/gbolahan/sites/spotvilla
Application Trace | Framework Trace | Full Trace

app/controllers/clistings_controller.rb:45:in new' app/controllers/clistings_controller.rb:45:increate’

I got the following error while trying to create a new set of listings.
i hve arelationship between the category and clisting model.

class Category < ActiveRecord::Base
belongs_to :clisting
end.

class Clisting < ActiveRecord::Base
has_one :category

end

pls help in sorting this out

On 22 November 2011 11:50, gbolahan a. [email protected] wrote:

ActiveRecord::AssociationTypeMismatch in ClistingsController#create

Category(#-612181558) expected, got String(#-608036348)

Did you read the replies that were posted to the last time you asked
this question?

On 22 November 2011 11:57, gbolahan a. [email protected] wrote:

yes i seemed to get it sorted the other time but the error is coming up
again, in need to understand what might be causing this.

The same thing… you’re passing a string to a method that expects an
associated object.

yes i seemed to get it sorted the other time but the error is coming up
again, in need to understand what might be causing this.

could you help me I how to identify where i am passing a string method
instead of the associated object.

On 22 November 2011 12:24, gbolahan a. [email protected] wrote:

could you help me I how to identify where i am passing a string method
instead of the associated object.

As I said before, you’ve probably got a form, with a field (probably a
select drop-down) that you’ve called “category”, which should be
called “category_id”.

Inspect your params data, and see if what is being passed to
update_attributes (I assume that’s what’s at line 45 of the
clistings_controller) is what you expect.

On 22 November 2011 12:24, gbolahan a. [email protected] wrote:

could you help me I how to identify where i am passing a string method
instead of the associated object.

Look at the error you posted, it is telling you the file and line number

Colin


gplus.to/clanlaw

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs