-1

I am not sure what the best way to describe this is (which is why I have had problems finding an answer on my own), but what I want to do is:

I have a model "Legislature", which each user can create.

Each legislature has several "committees". However, this list of committees needs to be the same for each legislature, and each has it's own set of columns with pre-set variables.

So I have:

Committee A Committee B Committee C

And each has it's own pre-determined fields. Each version of "legislature" will also have Committee A, Committee B, Committee C. As time moves on, each legislature's activities will dictate how the fields in the committees change, but of course, Committee A in one legislature may end up looking different than Committee A in another one.

How would I set this up? Alternately, if there is a term for what I am doing that I can use to google a solution, I would be fine with that as well!

mph372
  • 25
  • 5

1 Answers1

0

There are three Rails tools that may be of use: associations, callbacks, and scopes.

Associations: You can create a Legislature object and several Committee objects and leverage the has_many and belongs_to associations to reference these relationships. See https://guides.rubyonrails.org/association_basics.html

Callbacks: You can require that certain actions are taken at different points in an objects life cycle; this would allow for the creation of each requisite Committee when creating a Legislature. See https://guides.rubyonrails.org/active_record_callbacks.html.

Validations: By pairing the uniqueness validation, you can ensure that a single Legislature does not have Committee objects with identical features (e.g. name); with the scope option, you can further allow multiple Committee objects to have the same feature, provided that they each belong_to a distinct Legislature. See https://guides.rubyonrails.org/active_record_validations.html#uniqueness

Finally, this StackOverflow question may also be relevant: Validate uniqueness of in association

Jarmo
  • 191
  • 1
  • 10