I have a rails model set up as:
class Master < ActiveRecord::Base
belongs_to :user
has_many :submasters
serialize :subdocs, Array
end
It has a serialized array of submaster_docs which stores the id of the connected subdocs and my model for Subdocs is:
class Subdocs < ActiveRecord::Base
belongs_to :user
end
Now I have a rails method which deletes a subdoc when user clicks on the delete button.
While deleting the Subdocs entries I want to remove the id of the Subdocs from Master also so that it doesn't try to create an association between them even after deleting Subdocs
My database entry of Master looks as follows:
<ActiveRecord::Relation [#<Master id: 3, user_id: 1, name: "Being Batman", description: "Every man who has lotted here over the centuries, ...", subdocs: ["5"]>]>
Database entry of subdoc:
<ActiveRecord::Relation [#<Subdoc id: 5, user_id: 1, name: "subdoc1.pdf">]>
Over here for example if a user deletes the subdoc 5 then I want to remove that value from subdocs array of all Master's
My method for deleting the subdocs is as follows:
def destroy
@masters = {}
@subdoc = Subdoc.find(params[:id])
@masters = current_user.user.masters
# Tried these methods to delete the value.
# @subdocs = @masters.select { |t| t.subdocs.to_a - [params[:id]] }
# @subdocs = @masters.each { |t| t[:subdocs] - params[:id] }
@subdoc.destroy
return render :status => 200, :json => { :success => true }
end
But this throws me the error: No Implicit conversion of String into Array
How can I correct this? Thanks!