Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to update values #7

Open
sqlninja opened this issue Sep 26, 2020 · 7 comments
Open

Unable to update values #7

sqlninja opened this issue Sep 26, 2020 · 7 comments

Comments

@sqlninja
Copy link
Contributor

Using Rails 6, ActiveRecord, and Postgres

After retrieving an existing model from the database, I updated the model to have a "custom" field:

tc.update(
  leafy_fields_attributes: [
    { name: "hs_city", type: :string}
    ]  
) 

Although, upon trying to pull the values (which I'd expect to be nil) I get an undefined method 'leafy_values' error. So no getter...
So I thought perhaps I had to assign values (even though that shouldn't be the case)

tc.leafy_values ={ "hs_city": 'Gallatin'}

But this threw basically the same error undefined method 'leafy_values=' so no setter...

But indeed the object did store the "custom" field:

tc.leafy_fields
=> #<Leafy::Schema:0x00007fcc0875f400 @fields=[#<Leafy::Field:0x00007fcc0875f3b0 @id="hs_city-eed709f9-fea3-47ad-97e1-5e81ef800a66", @metadata={}, @name="hs_city", @type="string">]>

Am I missing something from the Read me? Or perhaps another initializer/config?

@estepnv
Copy link
Owner

estepnv commented Sep 28, 2020

Hey @sqlninja I was not testing it against rails 6. If you’ll find anything meanwhile pls create a pr. I’ll check it at the evening MSK

@estepnv
Copy link
Owner

estepnv commented Sep 28, 2020

@sqlninja Also pls make sure to use it like

class MyPreciousModel < ActiveRecord::Base
  include Leafy::Mixin::Schema[:active_record]
  self.leafy_data_attribute = :my_json_or_jsonb_attribute
end

so basically that's all

If you will still have issues with it please post the error backtrace in the comment below.

@estepnv
Copy link
Owner

estepnv commented Sep 28, 2020

@sqlninja
Copy link
Contributor Author

sqlninja commented Sep 29, 2020

So I had my model already declared the way you stated:

class TeamContact < ApplicationRecord
  include Leafy::Mixin::Schema[:active_record]
  ...
end

although I didn't have the leafy_data_attribute declared; adding that didn't resolve the issue either.
After adding:

class TeamContact < ApplicationRecord
  include Leafy::Mixin::Schema[:active_record]

  self.leafy_data_attribute = :leafy_data
...
end

I still get:

NoMethodError: undefined method `leafy_values=' for #<TeamContact:0x00007fa5b01e2960>

This is my simple test that I am running via rails console:

tc = TeamContact.last

tc.update(
  leafy_fields_attributes: [
    { name: "hs_city", type: :string}
    ]  
)  

tc.leafy_values = { "hs_city": 'Gallatin'}

the entire error is:

NoMethodError: undefined method `leafy_values=' for #<TeamContact:0x00007fa5b01e2960>
Did you mean?  leafy_fields=
from /Users/johnhenderson/.rvm/gems/ruby-2.6.5@stacked-sports/gems/activemodel-6.0.3.1/lib/active_model/attribute_methods.rb:432:in `method_missing'

@estepnv
Copy link
Owner

estepnv commented Sep 29, 2020

@sqlninja hey there. Looks like you are using the same model for both hosting fields definition and storing values.
The library implies the usage of two separate models, however you can try including both schema mixin and fields mixin. You’ll have to add two separate fields to store the data, one for the fields definition, another one for values. However I’m not sure that it’s going to work, cuz I was not planning to use it this way

@sqlninja
Copy link
Contributor Author

Ok I am concerned this just doesn't work in ActiveRecord...
Based on this file

If following the readme, it seems to indicate that you can call:

target.leafy_values = { "id_1": 123, "id_2": "test", "id_3": Time.new(2018,10,10, 10,10,10, "+03:00"), "junk": "some junk data" }

yet that file above seems to show that calling leafy_values would ultimately callleafy_field_values which makes a reference to leafy_fields in:

field_value_collection = ::Leafy::FieldValueCollection.new(leafy_fields, ar_json: activerecord_json_column?)

but in that file it raises an error:

def leafy_fields
  raise(RuntimeError, "Leafy: leafy_fields method is not defined")
end

so regardless of trying to use this in one model, this just doesn't seem to work.

Again, am I just missing something obvious?

Thanks

@sqlninja
Copy link
Contributor Author

sqlninja commented Oct 3, 2020

pinging...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants