Coder Social home page Coder Social logo

Nested Attributes? about store_model HOT 22 OPEN

dmitrytsepelev avatar dmitrytsepelev commented on May 24, 2024
Nested Attributes?

from store_model.

Comments (22)

evanshabsove avatar evanshabsove commented on May 24, 2024 2

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.

DmitryTsepelev avatar DmitryTsepelev commented on May 24, 2024 1

Rolled out 0.4.1 with fixes for both issues @exsemt @jsice

from store_model.

DmitryTsepelev avatar DmitryTsepelev commented on May 24, 2024

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.

exsemt avatar exsemt commented on May 24, 2024

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.

DmitryTsepelev avatar DmitryTsepelev commented on May 24, 2024

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.

DmitryTsepelev avatar DmitryTsepelev commented on May 24, 2024

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.

exsemt avatar exsemt commented on May 24, 2024

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.

DmitryTsepelev avatar DmitryTsepelev commented on May 24, 2024

Here we go #17 - could you please try it out before I merge it in?

from store_model.

DmitryTsepelev avatar DmitryTsepelev commented on May 24, 2024

Released 0.4.0 with accept_nested_attributes_for support

from store_model.

exsemt avatar exsemt commented on May 24, 2024

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.

DmitryTsepelev avatar DmitryTsepelev commented on May 24, 2024

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.

exsemt avatar exsemt commented on May 24, 2024

Thx! Works! 👍

from store_model.

jsice avatar jsice commented on May 24, 2024

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.

DmitryTsepelev avatar DmitryTsepelev commented on May 24, 2024

Hi @jsice!

Thanks for the report, I'll do my best to investigate this issue later this week

from store_model.

jsice avatar jsice commented on May 24, 2024

Thanks!

from store_model.

evanshabsove avatar evanshabsove commented on May 24, 2024

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.

FunkyloverOne avatar FunkyloverOne commented on May 24, 2024

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.

23tux avatar 23tux commented on May 24, 2024

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.

DmitryTsepelev avatar DmitryTsepelev commented on May 24, 2024

Right, this method was not ported 😞

from store_model.

23tux avatar 23tux commented on May 24, 2024

Is there any chance, this method gets ported in the next release?

from store_model.

DmitryTsepelev avatar DmitryTsepelev commented on May 24, 2024

Sure, I just need a volounteer and will be happy to review the PR! 🙂

from store_model.

alexandreh92 avatar alexandreh92 commented on May 24, 2024

any updates?

from store_model.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.