How to call methods from two separate models in Ruby Volt

97 Views Asked by At

I have one model (Group) associated with my GroupController, but it has many instances of another model (People) in it. I want to be able to create new instances of People from my GroupController and add it to an array. What is the best way to go about doing this? The following is an excerpt from the GroupController:

    class Group < Volt::ModelController
      field :people_in_group        

      def add_people(name)
        people_in_group = []
        person = People.new(name)
        people_in_group << person
      end

    end

The function breaks when I try to create a new person. Any advice?

2

There are 2 best solutions below

0
On

Try something like this:

class Person < Volt::Model
  field :name, String
  def initialize(a_name)
    @name = a_name
  end
end

class Group < Volt::Model
  has_many :people

  def add_people(name)
    people << Person.new(name)
  end

end
0
On

Is this supposed to be a model? If so it should inherit from Volt::Model not Volt::ModelController

Thanks!