# Association Error

**URL:** <https://www.ruby-forum.com/t/association-error/138606>\
**Category:** Rails\
**Created:** [May 25, 2008, 8:24pm UTC](https://www.ruby-forum.com/t/association-error/138606 "2008-05-25T20:24:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mxp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@mxp](https://www.ruby-forum.com/u/mxp)\
**Post date:** [May 25, 2008, 8:24pm UTC](https://www.ruby-forum.com/t/association-error/138606/1 "2008-05-25T20:24:47Z")

</div>

Hello,

I’m having a touch time with the implementation of as has\_many  
relationship between 3 models:

Here are my models files

product.rb contains(note that I’m extending the product model from the  
plugin substruct):

require\_dependency RAILS\_ROOT +  
“/vendor/plugins/substruct/app/models/product”

class Product \< Item

has\_many :videos

end

video\_displays.rb contains

class VideoDisplay \< ActiveRecord::Base  
has\_many :videos

# Required input

validates\_presence\_of :display\_name  
validates\_uniqueness\_of :display\_name

end

video.rb contains

class Video \< ActiveRecord::Base  
belongs\_to :video\_displays  
belongs\_to :products

validates\_uniqueness\_of :filename

end

I created using the MySQL administrator two foreign keys in the videos  
table out of columns i provided in the migration:

```
create_table :videos do |t|
  t.column "filename", :string
  t.column "video_display_id", :integer
  t.column "product_id", :integer
  t.timestamps
end

```

Finally I attempt to load a video object into the product and  
video\_displays models with the following code in my save action for the  
products\_controller.rb

```
@product.attributes = params[:product]
if @product.save
  # Save product tags
  # Our method doesn't save tags properly if the product doesn't

```

already exist.  
# Make sure it gets called after the product has an ID  
@product.tag\_ids = params[:product][:tag\_ids] if  
params[:product][:tag\_ids]  
# Load Video path names into product model  
video\_errors = []  
@display =  
VideoDisplay.find\_by\_display\_name(params[:video\_display])  
if (!@display.nil?)  
params[:video].each do |v|  
if v[:filename]  
new\_video =  
Video.find\_or\_create\_by\_filename(v[:filename])  
if new\_video.save  
@display.videos \<\< new\_video  
@product.videos \<\< new\_video  
else  
video\_errors.push(new\_video.filename)  
end  
end  
end  
else  
video\_errors.push(“Choosen display not found  
params[:video\_display]”)  
end  
…  
I get the following error:

ActiveRecord::AssociationTypeMismatch in Admin/productsController#save

Video expected, got Video  
RAILS\_ROOT: /Users/mkp/eCommerce/substruct\_rel\_1-0-a3

Application Trace | Framework Trace | Full Trace  
vendor/rails/activerecord/lib/active\_record/associations/association\_proxy.rb:150:in  
`raise_on_type_mismatch' vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:24:in`\<\<’  
vendor/rails/activerecord/lib/active\_record/associations/association\_collection.rb:23:in  
`each' vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:23:in`\<\<’  
vendor/rails/activerecord/lib/active\_record/connection\_adapters/abstract/database\_statements.rb:66:in  
`transaction' vendor/rails/activerecord/lib/active_record/transactions.rb:80:in`transaction’  
vendor/rails/activerecord/lib/active\_record/transactions.rb:100:in  
`transaction' vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:22:in`\<\<’  
app/controllers/admin/products\_controller.rb:48:in `save' app/controllers/admin/products_controller.rb:43:in`each’  
app/controllers/admin/products\_controller.rb:43:in `save’

line 48 is  
@product.videos \<\< new\_video  
so my association with display\_videos is cool. When I check the videos  
table with mysql, it shows the new row created with the video\_display\_id  
properly set to the desired id in the video\_displays table. However, the  
association with products is not happening. Here are my hunches:

The Product model is an extension of the a model found in my substruct  
plugin which itself is derived from a model called Item. I suspect that  
this complication is confound RAILS somehow?!?

I created the foreign key association outside to RAILS using the MySQL  
administrator gui which I suspect may not be compatible with the RAILS  
interface:

When I startup script/server, I get the following warning:  
WARNING: You’re using the Ruby-based MySQL library that ships with  
Rails. This library is not suited for production. Please install the  
C-based MySQL library instead (gem install mysql). Maybe this has  
something to do with the problem.

I"m new to all this so please, any comments would be helpful and greatly  
appreciated.

Thank you for your attention,  
Marc

---

<div class="post-metadata">

**Author:** ![mxp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@mxp](https://www.ruby-forum.com/u/mxp)\
**Post date:** [May 25, 2008, 11:51pm UTC](https://www.ruby-forum.com/t/association-error/138606/2 "2008-05-25T23:51:30Z")

</div>

On 25 May 2008, at 19:24, Marc Pestana wrote:

> this complication is confound RAILS somehow?!?

Yes and no. The problem is that your classes are reloaded once per  
request, but (by default) plugins are not reloaded. The product class  
references the Video class via the association. but because of the  
reloading it ends up referencing a dead unloaded copy of the Video  
class. The easiest way around this in development mode (it won’t  
happen at all in production) is to remove the plugin from  
Dependencies.load\_once\_paths.

> I created the foreign key association outside to RAILS using the MySQL  
> administrator gui which I suspect may not be compatible with the RAILS  
> interface:
> 
> When I startup script/server, I get the following warning:  
> WARNING: You’re using the Ruby-based MySQL library that ships with  
> Rails. This library is not suited for production. Please install the  
> C-based MySQL library instead (gem install mysql). Maybe this has  
> something to do with the problem.

Irrelevant, although I do recommend that you do as the message advises.

Fred
