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

support has_many in YAML sources #94

Open
Nowaker opened this issue Sep 7, 2014 · 7 comments
Open

support has_many in YAML sources #94

Nowaker opened this issue Sep 7, 2014 · 7 comments

Comments

@Nowaker
Copy link
Contributor

Nowaker commented Sep 7, 2014

class Plans::IsoDistro < ActiveYaml::Base
  include ActiveHash::Associations
  has_many :iso_images, class_name: 'Plans::IsoImage'
end

class Plans::IsoImage < ActiveHash::Base
  include ActiveHash::Associations
  belongs_to :iso_distro, class_name: 'Plans::IsoDistro'
end
- id: 1
  code: arch
  name: Arch Linux
  iso_images:
    - path: archlinux.iso
      bit: 64
    - path: ubuntu.iso
      bit: 32

@isos.first
=> #<Plans::IsoDistro:0x007f75b8962348 @attributes={:id=>1, :code=>"arch", :name=>"Arch Linux", :iso_images=>[{"path"=>"archlinux.iso", "bit"=>64}, {"path"=>"ubuntu.iso", "bit"=>32}]}>
@isos.first.iso_images
=> []

This could understand nested structures and create IsoImage objects too, along with an appropriate relation.

@syguer
Copy link
Collaborator

syguer commented Apr 5, 2017

I'll close this because long time is passed. If you need this again, please reopen.
Thanks.

@syguer syguer closed this as completed Apr 5, 2017
@Nowaker
Copy link
Contributor Author

Nowaker commented Apr 5, 2017

Non-collaborators can't reopen.

@syguer syguer reopened this Apr 5, 2017
@syguer
Copy link
Collaborator

syguer commented Apr 5, 2017

@Nowaker Oh, sorry. I've reopened!

@Justin-Maxwell
Copy link

FWIW - Not yet using ActiveHash, but probably will be by the end of today...

This would be very cool for something related to the use case that led me looking for a static activemodel solution. (Very limited number of records, data is effectively read-only, the column structure changes probably more often than the recordset)

In fact, we're already seeding config data into ActiveRecord for another part of the system via two-tier YAML much like OP presents, that might be easier to manage if in ActiveHash - in that case, there are two different sub-configuration-record types.

So as a newcomer, put this down as a vote for 'cool thing to add somewhere down the line maybe'

@fibrasek
Copy link

fibrasek commented Apr 1, 2019

This would be extremely helpful.

I'm trying a similar case, where I have two ActiveYaml::Base models with relations, example:

class PermissionGroupTemplate < ActiveYaml::Base
  ...
  has_many :permission_templates
  ...
end
class PermissionTemplate < ActiveYaml::Base
  ...
  belongs_to :permission_template_group
  ...
end

Hence those examples, an structure like this in the yaml files would be interesting:

# permission_group_templates.yml
- id: 1
  permission_template_ids: [1]
# permission_templates.yml
- id: 1
  permission_group_template_id: 1

idk if the current implementation could support something like that, but it would be useful

EDIT

After a bit tinkering, this actually works :)

@kbrock
Copy link
Collaborator

kbrock commented Apr 2, 2019

I'm surprised you need the permission_template_ids: [1] since the relationship was already defined in permission_group_templates

For my own future reference would you please share with us:

  1. Is permission_group_templates.yaml#permission_template_ids needed?
  2. Are there changes to active hash that need to be done? (or can we close?)

@fibrasek
Copy link

fibrasek commented Apr 2, 2019

@kbrock as I tested:

  1. No, it's not needed. But for better readability and understanding I think it's a good thing to have. With or without the _ids suffix work.
  2. I added include ActiveHash::Associations in the model, as the documentation states. Nothing more.

Also, I didn't tinker with aliases or shortcuts, and so far I don't know how they could work in this case.

Don't you thing this use case should be explicit in the documentation?

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

5 participants