简体   繁体   English

如何在Ruby Volt中从两个单独的模型调用方法

[英]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. 我有一个与GroupController关联的模型(组),但是其中有许多另一个模型(人)的实例。 I want to be able to create new instances of People from my GroupController and add it to an array. 我希望能够从我的GroupController创建People的新实例并将其添加到数组中。 What is the best way to go about doing this? 这样做的最佳方法是什么? The following is an excerpt from the GroupController: 以下是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 如果是这样,它应该继承自Volt :: Model而不是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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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