Ruby on Rails Scope Association

I have a Ruby on Rails application in which I have licenses, elements that can be licensed, and a table that lists two (what elements in which quantity are present in the license?). Similar to the items in the basket.

Some of the items will no longer be sold, but I intend to store them in the database. Then I created soft-delete and used the default scope for the template and for relations. But when I try to modify existing entries using the appropriate template, 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

In this way:

pry (main)> License.find (0) .license_items [0] .readonly?
=> true

Is there a way to make this ratio not just reading?

I have already tried adding readonly (false)at the end of the field has_manyto Licensewithout success.

+4
1

GitHub, Rails 4.0, 4.0.1. Rails, readonly (false) , :

class License <ActiveRecord :: Base
    has_many: license_items,
          -> {joins (: item) .where (items: {deleted: false}). readonly (false)},
          dependent:: destroy
end

Rails, Gemfile, bundle update.

, Rails, readonly: false find ( ):

License.find (1) .license_items.find (1, readonly: false) .update_attributes (amount: 5)
+5

Source: https://habr.com/ru/post/1691378/


All Articles