I have three models:
has_many :products
)has_many :product_fields
and belongs_to :store
belongs_to :product
)I'm using Sidekiq to update a column in product_fields called content
(When I create the model I insert a url in a column in the same model called second_content so the original content column is null on create, but then I am trying to update the content column in my worker to a new value. Here is my worker:
def perform(store_id)
store = Store.find(store_id)
store.products.each do |product|
if product.type_of == "Video"
video_url = URI.parse product.product_fields[0].second_content
video = open("LINK HERE").read
video = ActiveSupport::JSON.decode video
product.product_fields[0].update_attribute(:content, video)
end
end
end
and my controller's create action looks like:
def create
@store = Store.new(store_params)
respond_to do |format|
if @store.save
#Start video job
VideosWorker.perform_async(@store.id)
format.html { redirect_to @store, notice: 'Store was successfully created.' }
end
end
end
Everything seems to be working, except in the Sidekiq terminal I get the error:
undefined method `update_attribute' for #<ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_ProductField:0x0000010531bdd8>
Use:
product.product_fields.first.update_attribute(:content, video)
instead of:
product.product_fields[0].update_attribute(:content, video)
as you cannot call update_attribute
on a CollectionProxy
object also make sure video
is a string