how to add a new association to library model

54 Views Asked by At

I am using merit library in rails. And hope to add an association in Merit::Score::Point so that it has a has_one association with another model call ScoreWorkflow.

Below is my code. In this code, I hope to add an association so that I can add a has_one to the library model. However it does not work. Is there anything that like this that I can put some function/assoication to a library model. Thanks.

module Merit
  module Score
    class Point < Point
      has_one :score_workflow
    end
  end
end
1

There are 1 best solutions below

1
Nick M On
class ScoreWorkflow
      belongs_to :point
end

If you want it the other way around...

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

... and...

class ScoreWorkflow
      has_one :point
end

Sometimes you must specify the class names:

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

Also make sure to check your foreign keys if you use ActiveRecord, so they match the conventions.