Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何填充has_many:通过模型表_Ruby On Rails - Fatal编程技术网

Ruby on rails 如何填充has_many:通过模型表

Ruby on rails 如何填充has_many:通过模型表,ruby-on-rails,Ruby On Rails,我的模型: 因此,我的VIP是在流程的早期创建的,并位于VIP表中。当我提交新的事件表单时,我会在我的事件表中获得一个新条目。我想做的是在我提交新事件表单时,用一个新条目填充我的directors表。directors表需要我刚刚创建的事件的事件id,以及表单中select标记中的vip id 我想把它添加到create操作中 def create @director = Director.new if @event.save redirect_to organ

我的模型:

因此,我的VIP是在流程的早期创建的,并位于VIP表中。当我提交新的事件表单时,我会在我的事件表中获得一个新条目。我想做的是在我提交新事件表单时,用一个新条目填充我的directors表。directors表需要我刚刚创建的事件的事件id,以及表单中select标记中的vip id

我想把它添加到create操作中

def create  
    @director = Director.new

    if @event.save
      redirect_to organization_path(@organization)
      @director.vip_id = @event.vip_id
      @director.event_id = @event.id
但这并没有在director表中创建条目。有没有一种方法是我没有看到的

  @director.vip_id = @event.vip_id
  @director.event_id = @event.id
因此,您有两个任务,@director.event\u id=@event.id这一个很好,因为您刚刚创建了事件,并且您有@event.id

但是,第一个不起作用。查看您的模型关联,@event没有vip\u id,因此您不能呼叫@event.vip\u id。您必须通过组织转到vip,例如:

@event.organization.vips.first.vip_id

或者,@event.vips.first.vip\u id这也会像您所拥有的那样:通过=>:董事会

这两个是我看到的获得与活动对应的有效vip_id的唯一方法。尽管如此,这将只获取第一个vip_id,或者您可以在where子句中指定一些条件来获取该事件的特定vip_id

如果这对你有用,那很好。否则,您可能需要重新考虑模型之间的关联

    def new
        @organization = Organization.find(params[:organization_id])
        @event = @organization.events.new
        @director = Director.new
    end

    def create
        @organization = Organization.find(params[:organization_id])
        @event = @organization.events.build(event_params)

        if @event.save 
            redirect_to organization_path(@organization)
        else
            render 'new'
        end
    end
def create  
    @director = Director.new

    if @event.save
      redirect_to organization_path(@organization)
      @director.vip_id = @event.vip_id
      @director.event_id = @event.id
  @director.vip_id = @event.vip_id
  @director.event_id = @event.id