has_many 到附加数据:计数

发布于 2024-10-11 06:57:33 字数 1044 浏览 4 评论 0原文

我试图找出处理连接表中存储同一对象数量的字段的最佳方法。

Schema

class Element < ActiveRecord::Base
   has_many :connections
   has_many :connector_types, :through => :connections
end

class ConnectorType < ActiveRecord::Base
  has_many :connections
  has_many :elements, :through => :connections
end

class Connection < ActiveRecord::Base
  belongs_to :element
  belongs_to :connector_type
end

当我将 ConnectorType 添加到 Element 时:

  • A <如果不存在此 ConnectorType,则必须创建 code>Connection
  • 如果存在此 ConnectorTypeConnection,则 Connection#number 应该递增

当我从 Element 中删除 ConnectorType 时:

  • Connection#number 应该递减。
  • 如果 Connection#number == 0 删除 Connection

我是 Rails 新手,我不知道 Rails 执行此操作的方法:

  • 回调
  • 转储数字字段并存储为重复项连接模型中的行。
  • ...

I am trying to figure out the best way to handle a field storing the quantity of the same object in my join table.

Schema

class Element < ActiveRecord::Base
   has_many :connections
   has_many :connector_types, :through => :connections
end

class ConnectorType < ActiveRecord::Base
  has_many :connections
  has_many :elements, :through => :connections
end

class Connection < ActiveRecord::Base
  belongs_to :element
  belongs_to :connector_type
end

When I add a ConnectorType to an Element :

  • A Connection must be created if there isn't for this ConnectorType
  • If a Connection for this ConnectorType exists, Connection#number should be increment

When I remove a ConnectorType from an Element :

  • Connection#number should be decrement.
  • If Connection#number == 0 delete the Connection

I am new to rails I don't know the Rails way to do this :

  • Callback
  • Dump the number field and store as duplicate rows in the join model.
  • ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小巷里的女流氓 2024-10-18 06:57:33

如果我理解得很好,您想要监视给定元素和给定connector_type之间的连接数,但您不希望数据库中存在重复的Connection对象?

您可以为此使用回调(代码未测试)

# connection.rb 
before_create  :bc_callback
before_destroy :bd_callback


private
def before_create
  if (existing_connection = self.find_by_element_id_and_connector_type_id(element_id, connector_type_id))
    existing_connection.number++
    existing_connection.save
    return false # Do not create a new connection object
  end
end

def before_destroy
  number--
  # If you still have 1 connection or more, the object won't be destroyed
  if (number > 0)
    save
    return false
  end
end

但是,我不确定通过这样的 has_many 关系向元素添加connector_type

@element.connector_types << @connector_type

是否会触发新连接的创建,如果已经有一个链接该元素并且该连接器类型...

If I understand well, you want to monitor the number of connections between a given element and a given connector_type, but you do not want to have duplicate Connection objects in your database?

You can use callbacks for this (code not tested)

# connection.rb 
before_create  :bc_callback
before_destroy :bd_callback


private
def before_create
  if (existing_connection = self.find_by_element_id_and_connector_type_id(element_id, connector_type_id))
    existing_connection.number++
    existing_connection.save
    return false # Do not create a new connection object
  end
end

def before_destroy
  number--
  # If you still have 1 connection or more, the object won't be destroyed
  if (number > 0)
    save
    return false
  end
end

However, I'm not sure that adding a connector_type to an element via the has_many relation like this:

@element.connector_types << @connector_type

will trigger the creation of a new connection, if there already is one linking this element and this connector type...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文