【发布时间】:2015-12-02 02:21:03
【问题描述】:
以前有人问过类似的问题,但我就是想不通。
所以我有 Model_A 和 Model_B。模型_B belongs_to 模型_A。我想做的是在创建Model_A时自动为Model B调用create方法。然后一个脚本接管a为Model_B生成一堆数据。我使用after_create,因为这只需要发生一次。
它需要这样做。如果您想了解详细信息,请随时询问...
所以我在这里找到了 Model_A。我似乎无法在create_model_b 中获得正确的语法。在我使用的示例中,我只是收到一条错误消息,提示 Model_A 不存在该方法。
class Model_A < ActiveRecord::Base
belongs_to :Model_B
after_create :create_model_b
...
def create_model_b
#so I tried a bunch of stuff here but none of it worked
#I need to create a Model_B which will contain the current Model_A id
#ex. self.model_b.create(model_a_id: self.id)
end
end
Model_B 并没有做任何特别的事情:
class Model_B < ApplicationController
def create
@model_b = Model_B.new(model_b_params)
create_the_data
respond_to do |format|
if @model_b.save
#redirect
else
#uh oh
end
end
end
end
谢谢!
【问题讨论】:
标签: mysql ruby-on-rails database belongs-to