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