简体   繁体   中英

How to call methods from two separate models in Ruby Volt

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?

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

Thanks!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM