如何在库模型中添加新的关联



我正在rails中使用择优库。并希望在Merit::Score::Point中添加一个关联,以便它与另一个名为ScoreWorkflow的模型有一个关联。

下面是我的代码。在这段代码中,我希望添加一个关联,以便可以向库模型添加has_one。但是它不起作用。有这样的东西吗?我可以把一些函数/关联放到库模型中。谢谢

module Merit
  module Score
    class Point < Point
      has_one :score_workflow
    end
  end
end
class ScoreWorkflow
      belongs_to :point
end

如果你想反过来。。。

module Merit
  module Score
    class Point < Point
      belongs_to :score_workflow
    end
  end
end

class ScoreWorkflow
      has_one :point
end

有时您必须指定类名:

module Merit
  module Score
      class Point < Point
        has_one :score_workflow, :class_name => "ScoreWorkflow"
      end
  end
end

class ScoreWorkflow
    belongs_to :point, :class_name => "Merit::Score::Point"
end

如果您使用ActiveRecord,请确保检查外键,以便它们符合约定。

最新更新