opened
vikks
username_0
unknown attribute error when used with jsonb attributes
issue
created
antstorm
username_1
@username_0 can you point to the file/line that throws the error?
comment
created
vikks
username_0
##### StackTrace

```ruby
   (0.2ms)  BEGIN
   (0.1ms)  ROLLBACK
Completed 500 Internal Server Error in 66ms (ActiveRecord: 10.8ms)

ActiveModel::MissingAttributeError (can't write unknown attribute `price_fils`):
  
app/controllers/products_controller.rb:32:in `update'
  Rendering /Users/vikas/.rvm/gems/ruby-2.3.0/gems/actionpack-5.0.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout
  Rendering /Users/vikas/.rvm/gems/ruby-2.3.0/gems/actionpack-5.0.1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
(...)
```
##### in products_controller.rb

```ruby
def update 
   @product = Product.find params[:id]
   @product.update product_params
end

# params = {"product"=>{"price"=>"100"}, "commit"=>"Update", "id"=>"13"}
def product_params
   params.require(:product).permit!
end
```
comment
created
antstorm
username_1
@username_0 thanks, I'll have a closer look
comment
created
antstorm
username_1
@username_0 it seems like this behaviour is not supported, because `money-rails` uses `write_attribute` when modifying values (not the setter). You can still use the gem, but you'll need to modify your setter to something like:

```ruby
def price=(new_value)
  properties[:price_cents] = Money.new(new_value).fractional
end
```

Hope that helps.
comment
created
swrobel
username_2
Alternatively, it seems you can do the following:

```ruby
class Product < ApplicationRecord
  # properties is a jsonb column in postgreql
  store_accessor :properties, :price_cents
  monetize :price_cents, disable_validation: true
  validates :price, money: { greater_than_or_equal_to: 0 }
end
```

Since bizarrely, the subunit (_cents column) setter is only overwritten with `write_attribute` [when validations are enabled](https://github.com/RubyMoney/money-rails/blob/v1.10.0/lib/money-rails/active_record/monetizable.rb#L130).
comment
created
antstorm
username_1
@username_2 good point, I guess we can override it anyways
comment
closed
semmons99
username_3
issue