ruby-on-rails - 关系和setter方法

标签 ruby-on-rails ruby neo4j neo4j.rb

我正在尝试重写一个已声明关系的 setter 方法,以在旧节点和新节点上执行自定义行为和一些验证。

例如,举下一个例子:

class FantasticTeam 
  include Neo4j::ActiveNode

  has_one :in, :active, model_class: Whatever
  has_many :in, :not_active, model_class: Whatever

  def active=(whatever)
    # return if whatever is not include in :not_active
    # add the old value to :not_active
    # delete the relationship :not_active that belongs to whatever (the param)
    # add :active relationship to whatever
  end
end

做这样的事情的正确方法是什么?

最佳答案

有两种方法可以处理这个问题,这两种方法都比覆盖 setter 方法更容易。

第一个是在您的 ActiveNode 模型中使用回调。

class FantasticTeam 
  include Neo4j::ActiveNode

  has_one :in, :active, before: :before_callback, model_class: Whatever
  has_many :in, :not_active, model_class: Whatever

  private

  def before_callback(other)
    return false unless self.not_active.include?(other)
    self.not_active(:w, :r).where(uuid: other.uuid).pluck(:r).each { |r| r.destroy }
    self.not_active << other unless self.active.nil?
  end
end

我个人从不使用该过程。我总是将 ActiveRel 模型用于任何关系逻辑。

class FantasticTeam 
  include Neo4j::ActiveNode

  has_one :in, :active, model_class: 'Whatever', rel_class: 'FantasticActiveWhatever'
  has_many :in, :not_active, model_class: 'Whatever'

end

class FantasticActiveWhatever
  include Neo4j::ActiveRel
  from_class FantasticTeam
  to_class Whatever
  type 'ACTIVE'

  validate      :inactive
  before_create :clear_inactive

  private

  def inactive
    self.errors.add(:inactive, 'Destination node must be declared inactive') unless from_node.not_active.include?(to_node)
  end

  def clear_inactive
    from_node.not_active(:w, :r).where(uuid: to_node.uuid).pluck(:r).each { |r| r.destroy }
  end
end

然后你会创建:

rel = FantasticActiveWhatever.new(from_node: team, to_node: whatever) 如果相对保存 # 继续前行 结束

ActiveRel 需要一些额外的设置,但它的功能要强大得多。

无论哪种方式,这都是需要包装在事务中的东西,因为您有可能删除 not_active 关系但无法设置 active

begin
  tx = Neo4j::Transaction.new
  # the whole process
rescue StandardError
  tx.failure
  # additional failure behavior, if any
ensure
  tx.close
end

基本关系回调记录在 https://github.com/neo4jrb/neo4j/wiki/Neo4j-v3-Declared-Relationships#relationship-callbacks .

ActiveRel 记录在 https://github.com/neo4jrb/neo4j/wiki/Neo4j%3A%3AActiveRel .

交易记录在 https://github.com/neo4jrb/neo4j/wiki/Transaction .

关于ruby-on-rails - 关系和setter方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26439689/

相关文章:

ruby-on-rails - 生成没有路径的link_to(仅 anchor )

sql - 迭代大型外部 postgres db,操作行,将输出写入 rails postgres db

mysql - 连接到 Rails 中单独的 mysql 数据库

ruby-on-rails - 如何调试插件/gem? (附有设置和使用 ruby​​-debug gem 的有用说明)

ruby-on-rails - Rails ActiveAdmin,表单页面上的两列(不是边栏)

javascript - "Listening"到来自 Capybara/Selenium 的 AJAX 调用 - Rails

ruby - 在 Ubuntu/Debian Linux 上安装 nokogiri

neo4j - 限制联合密码查询的结果

ruby-on-rails - Rails 4.0.0、Neo4j - Bundler 找不到 gem "railties"的兼容版本

jquery - neo4j - 创建不同的用户身份验证