I have a Ruby on Rails application in which I have licenses, items that can be licensed, and a table that lists the two (What items in what quantity are present in the license?). Analogously to the items in a shopping cart.
Some of the items will no longer be marketed, but I want to keep them in the database. I then created a soft-delete and used a default scope for the template and for the relationships. But when I try to change existing records using the related model, I get an exception: ActiveRecord::ReadOnlyRecord
My templates look like this:
class Item < ActiveRecord::Base
default_scope { where(deleted:false) }
end
class LicenseItem < ActiveRecord::Base
belongs_to :license, touch: true
belongs_to :item
end
class License < ActiveRecord::Base
has_many :license_items,
-> { joins(:item).where(items: {deleted: false} ) },
dependent: :destroy
end
This way:
pry(main)> License.find(0).license_items[0].readonly?
=> true
Is there any way to make this relationship not just read?
I have tried to add readonly(false)
at the end of the scope of has_many
to License
to no avail.