跟踪用户在数据库中喜欢的事件

时间:2022-06-01 18:17:54

I'm looking to incorporate a button to where users can click it and add an event to their list of events. The events they added to their list will then be populated on another view.

我希望在用户可以点击它的位置添加一个按钮,并将事件添加到他们的事件列表中。然后,他们添加到列表中的事件将填充到另一个视图中。

I'm wondering what the best way to implement this is.

我想知道实现这个的最佳方法是什么。

I've considered using a serialized text column in my table and just pushing the event's id to the array. Is the best way of going about doing this and do I still have functionality to operate on the array?

我已经考虑在我的表中使用序列化文本列,只是将事件的id推送到数组。这是实现这一目标的最佳方式吗?我还有在阵列上运行的功能吗?

1 个解决方案

#1


0  

What you're looking for is associations, specifically the has_and_belongs_to_many association.

您正在寻找的是关联,特别是has_and_belongs_to_many关联。

http://guides.rubyonrails.org/association_basics.html

http://guides.rubyonrails.org/association_basics.html

I remember having the exact same thought as you when I was starting out - use a column in the model to act as a bucket which stores all the IDs of another model. While this is technically possible using serialize, it's not at all ideal as searching through a serialized column is extremely inefficient. PostgreSQL has the hstore data type which would greatly speed up searching, but it's still not ideal.

我记得当我刚刚开始时有与你完全相同的想法 - 使用模型中的一列作为存储其他模型的所有ID的存储桶。虽然这在技术上可以使用序列化,但它并不理想,因为搜索序列化列的效率非常低。 PostgreSQL具有hstore数据类型,可以大大加快搜索速度,但它仍然不理想。

Associations in Rails are worked right into the framework and learning how to use them is vital to your future success as a rails developer. That guide will explain it better than I ever could. If you're getting stuck, I'd start out by doing a simple has_many and belongs_to relationship between Users and Events in a playground application. Then implement has_and_belongs_to_many in your production app.

Rails中的关联可以直接进入框架,学习如何使用它们对于您作为rails开发人员的未来成功至关重要。该指南将比我更好地解释它。如果你遇到困难,我首先要在游乐场应用程序中的用户和事件之间做一个简单的has_many和belongs_to关系。然后在生产应用中实现has_and_belongs_to_many。

#1


0  

What you're looking for is associations, specifically the has_and_belongs_to_many association.

您正在寻找的是关联,特别是has_and_belongs_to_many关联。

http://guides.rubyonrails.org/association_basics.html

http://guides.rubyonrails.org/association_basics.html

I remember having the exact same thought as you when I was starting out - use a column in the model to act as a bucket which stores all the IDs of another model. While this is technically possible using serialize, it's not at all ideal as searching through a serialized column is extremely inefficient. PostgreSQL has the hstore data type which would greatly speed up searching, but it's still not ideal.

我记得当我刚刚开始时有与你完全相同的想法 - 使用模型中的一列作为存储其他模型的所有ID的存储桶。虽然这在技术上可以使用序列化,但它并不理想,因为搜索序列化列的效率非常低。 PostgreSQL具有hstore数据类型,可以大大加快搜索速度,但它仍然不理想。

Associations in Rails are worked right into the framework and learning how to use them is vital to your future success as a rails developer. That guide will explain it better than I ever could. If you're getting stuck, I'd start out by doing a simple has_many and belongs_to relationship between Users and Events in a playground application. Then implement has_and_belongs_to_many in your production app.

Rails中的关联可以直接进入框架,学习如何使用它们对于您作为rails开发人员的未来成功至关重要。该指南将比我更好地解释它。如果你遇到困难,我首先要在游乐场应用程序中的用户和事件之间做一个简单的has_many和belongs_to关系。然后在生产应用中实现has_and_belongs_to_many。