Comments (22)
I ended up figuring it out! I was trying to be too cute with it. We use Inherited Resources (https://github.com/activeadmin/inherited_resources) and I was hoping the update! method in that would magically take care of everything, but I was wishing too hard. Changing the assignment to just
@pathogen.configuration = configuration_params[:configuration].to_hash
Fixed everything. @FunkyloverOne thanks for linking me to the issue thread!
Looking at this though I wonder if I just remove the :configuration part of the param I can be cute with it...anyways a discussion for another place.
from store_model.
Rolled out 0.4.1 with fixes for both issues @exsemt @jsice
from store_model.
Hi @exsemt!
Please take a look at the discussion in this issue #7, where we've discussed an ability to update store_model
as a nested association. Do you want the same thing or you have one store_model
inside another?
from store_model.
Yes, I have one store_model
inside another:
class Concept
include StoreModel::Model
attribute :id, :integer
attribute :name, :string
attribute :values, ConceptValue.to_array_type
# fix for nested attributes
def values_attributes=(values)
self.values = values.values
end
end
class ConceptValue
include StoreModel::Model
attribute :name, :string
...
end
It will be greate to have some like accept_nested_attributes_for
and not to write values_attributes=
from store_model.
Got it, that sounds like a nice addition 🙂 Feel free to come up with a PR if you're interested in playing with the gem, otherwise I'll take a look at it next week
from store_model.
Hi @exsemt!
I've added a couple of tests for nested attributes, and it looks like for your case
concept.update(id: 1, name: "name", values: [{ name: "name }])
or
concept = Concept.last
concept.values = [ConceptValue.new(name: "name)]
concept.values
should work of the box. What usecase am I missing here?
from store_model.
Correct, that works, but it does not work as an Active Record Nested Attributes
so values_attributes
:
concept.update(id: 1, name: "name", values_attributes: [{ name: "name }])
from store_model.
Here we go #17 - could you please try it out before I merge it in?
from store_model.
Released 0.4.0 with accept_nested_attributes_for
support
from store_model.
Thanks! Sorry that took so long time to check it, it works fine for .to_type
association, but it does not work for a .to_array_type
, because you get from HTML form a hash with a key as counter of association object and value - the object hash. e.g.:
class Concept
include StoreModel::Model
attribute :id, :integer
attribute :name, :string
attribute :values, ConceptValue.to_array_type
end
# does not work
concept.update(id: 1, name: "name", values_attributes: [{ "0" => { "name" => "name 1" }}, { "1" => { "name" => "name 2" }}])
from store_model.
Here is a PR with a possible fix. The only thing is that in Rails only Hash and Array are accepted, so the param should be either { "0" => { "name" => "name 1" }, "1" => { "name" => "name 2" }]
or [{ "name" => "name 1" }, { "1" => { "name" => "name 2" }]
. Could you please make sure your form sends [{ "0" => { "name" => "name 1" }}, { "1" => { "name" => "name 2" }}]
?
from store_model.
Thx! Works! 👍
from store_model.
I tried adding validatation on nested attributes but it gave me the error "NoMethodError (undefined method `type_for_attribute' for ...)"
class Form
include StoreModel::Model
attribute :id, :integer
attribute :for, :string
attribute :inputs, FormInput.to_array_type
validates :id, :for, presence: true
validates :inputs, store_model: true
end
class FormInput
include StoreModel::Model
attribute :label, :string
attribute :value, :string
validates :label, :value, presence: true
end
from store_model.
Hi @jsice!
Thanks for the report, I'll do my best to investigate this issue later this week
from store_model.
Thanks!
from store_model.
Hey all, firstly this is a really cool gem and it will hopefully make my life way easier! I've just started playing around with it and have the following set up
class Pathogen < ActiveRecord::Base
attribute :configuration, Configuration.to_type
end
class Configuration
include StoreModel::Model
accepts_nested_attributes_for :susceptibility
attribute :susceptibility, Susceptibility.to_array_type
end
class Susceptibility
include StoreModel::Model
attribute :icon, :string
attribute :link, :string
attribute :name, :string
attribute :value, :string
attribute :render_method, :string
end
When I submit the update form on the pathogen my params look like
> pathogen_params[:configuration]
=> <ActionController::Parameters {"susceptibility_attributes"=><ActionController::Parameters {"0"=><ActionController::Parameters {"value"=>"Edit one", "name"=>"General susceptibility", "icon"=>"", "link"=>"", "render_method"=>"string"} permitted: true>, "1"=><ActionController::Parameters {"value"=>"Edit two", "name"=>"Antibiogram footnotes", "icon"=>"", "link"=>"", "render_method"=>"string"} permitted: true>} permitted: true>} permitted: true>
but when I call update on the pathogen model it does not save the nested susceptibility attributes and instead returns just nil
> Pathogen.find(1898).configuration
Pathogen Load (1.2ms) SELECT "pathogens".* FROM "pathogens" WHERE "pathogens"."id" = $1 LIMIT $2 /*application:Spectrum*/ [["id", 1898], ["LIMIT", 1]]
=> #<Configuration susceptibility: nil, general_information: nil>
I'm thinking I am just doing something small wrong...any help would be greatly appreciated!
from store_model.
Hey @evanshabsove, I'm not sure it will help, but just in case, take a look here: #41
I didn't even know there is an accept_nested_attributes_for
support! Gotta play around with it :)
from store_model.
I know this is an old issue, but I stumbled upon an error when using shoulda matchers:
Failure/Error: it { is_expected.to accept_nested_attributes_for(:parcels) }
NoMethodError:
undefined method `nested_attributes_options' for SomeModel:Class
# /usr/local/bundle/gems/shoulda-matchers-5.1.0/lib/shoulda/matchers/active_record/accept_nested_attributes_for_matcher.rb:196:in `model_config'
# /usr/local/bundle/gems/shoulda-matchers-5.1.0/lib/shoulda/matchers/active_record/accept_nested_attributes_for_matcher.rb:192:in `config'
# /usr/local/bundle/gems/shoulda-matchers-5.1.0/lib/shoulda/matchers/active_record/accept_nested_attributes_for_matcher.rb:152:in `exists?'
# /usr/local/bundle/gems/shoulda-matchers-5.1.0/lib/shoulda/matchers/active_record/accept_nested_attributes_for_matcher.rb:121:in `matches?'
This leads to this line https://github.com/thoughtbot/shoulda-matchers/blob/main/lib/shoulda/matchers/active_record/accept_nested_attributes_for_matcher.rb#L196
Is there something missing inside store model?
from store_model.
Right, this method was not ported 😞
from store_model.
Is there any chance, this method gets ported in the next release?
from store_model.
Sure, I just need a volounteer and will be happy to review the PR! 🙂
from store_model.
any updates?
from store_model.
Related Issues (20)
- Validate any Hash HOT 1
- Encrypt attributes HOT 2
- Aliasing an attribute HOT 1
- GraphQL input type fails to cast HOT 4
- ArgumentError: wrong number of arguments (given 1, expected 0) in random model HOT 3
- Assignment Doesn't work in OneOf case HOT 3
- Delegation of `fetch` to `attributes` causes issues HOT 2
- NameError: uninitialized constant StoreModel::Types::ArrayType HOT 3
- Default values for attributes when retrieving the store model HOT 1
- Decoding Custom Types HOT 1
- Allow an option to disallow "UnknownAttributes" behavior HOT 1
- ActiveRecord validation contexts don't propagate HOT 1
- Override methods HOT 3
- Broken defaults in 2.0 HOT 2
- Attribute encryption with ActiveRecord::Encryption HOT 1
- StoreModel Stringifying json when saving. HOT 14
- accepts_nested_attributes_for allow partial updates HOT 2
- [QUESTION] How preserve data on update? HOT 1
- JSON deserializing empty string, leads to nil error
- Optional tracking of parent functionality HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from store_model.