active-hash / active_hash

A readonly ActiveRecord-esque base class that lets you use a hash, a Yaml file or a custom file as the datasource
MIT License
1.21k stars 178 forks source link

support has_many in YAML sources #94

Open Nowaker opened 10 years ago

Nowaker commented 10 years ago
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 commented 7 years ago

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

Nowaker commented 7 years ago

Non-collaborators can't reopen.

syguer commented 7 years ago

@Nowaker Oh, sorry. I've reopened!

Justin-Maxwell commented 7 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

@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?