goal 的数据库存储方法 - mindpin/knowledge-space-net-lib GitHub Wiki
class Goal
has_meny :solutions
belongs_to :solution
@prev
@next
BEGIN = '::BEGIN'
END = '::END'
end
class Solution
has_many :goals
belongs_to :goal
end
这样做的目的是能够直接通过 Goal ID 定位到一个 Goal 以及能够直接从 Solution ID 定位到一个 Solution
同时需要实现 goal.to_xml 以及 goal.to_json
模型上应该有的方法
基础方法
# 根据 id 找到一个 goal
goal = Goal.find(goal_id)
# 获取 goal 的 父 solution
goal.solution
# 获取 goal 的 子 solutions
goal.solutions
# 获取 goal 的 prev
goal.prev # goal | nil | ::BEGIN | ::END 取值有四种可能
# 获取 goal 的 next
goal.next # goal | nil | ::BEGIN | ::END 取值有四种可能
# 根据 solution_id 获取 solution
solution = Solution.find(solution_id)
# 获取 solution 的 父 goal
solution.goal
# 获取 solution 的 子 goals
solution.goals
# 删除一个 solution
solution.destroy
一些较复杂的方法
# 获取 goal 的 json
goal.json
# 增加一个 子 solution(内容可以完全为空)
goal.create_solution()
# 删除一个 goal(删除后,要把自己的 prev 和 next 链接起来)
goal.destroy
# 增加一个 子 goal
solution.create_goal(
:name => "xxx",
:direction => "before" | "after",
:target => target_goal_id | "::BEGIN" | "::END"
)
# direction 和 target 的参数搭配逻辑如下
# before ::BEGIN 无效
# before ::END 在终点处添加新 goal 或者插入 goal
# before target_goal_id 在指定的 goal 之前添加新 goal 或者插入 goal
# after ::BEGIN 在起点处添加新 goal 或者插入 goal
# after ::END 无效
# after target_goal_id 在指定的 goal 之后添加新 goal 或者插入 goal