如何選擇一個單一的模型並堅持這個選擇?

[英]How to choose a single model and persist that choice?


I have a simple model called Party with a corresponding table called parties. There's also a controller with all the usual CRUD actions and so on. This model is used in a website and only one admin user is allowed to edit the parties - everyone else is allowed to call GET actions (index, show). Nothing special so far.

我有一個簡單的模型,叫做Party,對應的表格叫做parties。還有一個具有所有常規CRUD操作的控制器等等。該模型在網站中使用,只有一個管理員用戶可以編輯各方——其他人可以調用GET操作(index, show)。到目前為止沒有什么特別的。

Now I need to do the following: The admin would like to choose a single Party at a time for special presentation (the selected Party is showing up on the start page of the application). The most important thing is, that there's only ONE party at time selected.

現在我需要做以下工作:管理員希望每次選擇一個單獨的方進行特殊表示(所選的方顯示在應用程序的開始頁面上)。最重要的是,選擇的時候只有一個政黨。

How would you solve this problem? Boolean Flag in Party model? Save the selection (id of the party) somewhere outside the database? Implement a new model with a has_one relation to Party (seems like overkill to me)?

你如何解決這個問題?在黨的模型中布爾標志?將所選內容(方的id)保存在數據庫之外的某個地方?使用與Party的has_one關系實現一個新模型(對我來說似乎有些過分了)?

I hope my explanation is good enough to understand the issue.

我希望我的解釋能說明問題。

5 个解决方案

#1


3  

A simple "front_page" attribute would suffice or another model like you mentioned, using the has_one relationship would be fine as well.

一個簡單的“front_page”屬性就足夠了,或者像您提到的另一個模型,使用has_one關系也可以。

Using another model would allow you to maintain some more information, like how long should it remain on the front page (expiration date?) or how many times it was featured (assuming a party can be featured twice). It really depends on other requirements for your system.

使用另一個模型可以讓您維護更多的信息,比如它應該在首頁上停留多長時間(過期日期?),或者它被顯示了多少次(假設一個聚會可以被顯示兩次)。它確實依賴於系統的其他需求。

You also might be able to get away with a simple implementation of the Singleton pattern as well. There's a quick description on the Rails Wiki of making an ActiveRecord object a Singleton (see below): http://wiki.rubyonrails.org/rails/pages/TipsAndTricks

您還可以使用單例模式的簡單實現。在Rails的Wiki上有一個簡單的描述,可以讓ActiveRecord對象成為一個單例對象(參見下面):http://wiki.rubyonrails.org/rails/pages/TipsAndTricks。

Making a singleton ActiveRecord object

創建一個單例ActiveRecord對象

If you have a table with just one entry, useful for keeping track of a number sequence for databases without sequences, you can use the singleton module included with ruby like so:

如果您有一個只有一個條目的表,這對於跟蹤沒有序列的數據庫的數字序列很有用,那么您可以使用ruby中包含的單例模塊,如下所示:

require 'singleton'

class Master < ActiveRecord::Base
  include Singleton
  def initialize(args=nil) super(args) if record = Master.find(:first)    
    self.attributes = record.attributes end end def next_tracking_number increment!
    (:current_tracking_number) current_tracking_number end def 
    self.next_tracking_number instance.next_tracking_number 
  end
end

Update:

更新:

This is a very poor code example (was copied and pasted from the Rails Wiki, which had no formatting). I would highly recommend the [Ruby Design Patterns] book which tackles many of the GoF design patterns in greater detail (while making them applicable to Ruby applications). But Google should return you some good resources for using the Singleton pattern in Ruby.2

這是一個非常糟糕的代碼示例(從Rails Wiki中復制和粘貼,沒有格式)。我強烈推薦《Ruby設計模式》這本書,它更詳細地處理了許多GoF設計模式(同時使它們適用於Ruby應用程序)。但是谷歌應該會為使用Ruby.2中的單例模式返回一些好的資源

#2


2  

I would go for the boolean flag and create nested singleton resource (promoted), which I would implement in PartiesController itself (set_promoted_party and get_promoted_party actions). For these I would create two new routes:

我將使用布爾標志並創建嵌套的單例資源(提升),我將在PartiesController本身(set_promoted_party和get_promoted_party動作)中實現它。為此,我將創建兩條新路線:

PUT /parties/promoted/:party_id # to set the promoted party
GET /parties/promoted/:party_id # to get the promoted_party

#3


1  

I would add a second model that had a has_one relationship in order to keep the app RESTful and simple. Also, this way, you can keep a history of special Parties, and track other meaningful information related to the special parties.

我將添加第二個具有has_one關系的模型,以保持應用程序的RESTful性和簡單性。這樣,你就可以保持一個特殊的政黨的歷史,並追蹤與特殊政黨有關的其他有意義的信息。

#4


0  

Personally I'm very strong on data integrity being enforced by my database so would probably add that extra table and enforce it as a foreign key constraint there. It can seem like overkill, but is the only* solution that prevents data integrity issues.

就我個人而言,我對我的數據庫執行的數據完整性非常在行,因此可能會添加額外的表並將其作為外鍵約束來執行。這看起來有點過分了,但是這是防止數據完整性問題的唯一解決方案。

Could you maybe add it as a field to the admin table/model - which would be an enforced foreign key to the party table?

您是否可以將它作為一個字段添加到管理表/模型中——這將是party表的強制外鍵?

*Another solution would be a database trigger that checks no other rows are the selected party but I tend to shy away from such solutions.

*另一個解決方案是一個數據庫觸發器,它檢查沒有其他行是被選中的一方,但我傾向於回避這種解決方案。

#5


0  

Keep it simple. Put a promoted_party.yml file in your config directory that the controllers write to and read from. The contents can be as simple as this:

保持簡單。promoted_party。在配置目錄中的yml文件,控制器對其進行寫入和讀取。內容可以如此簡單:

--- 
party_id: 123

Done. If you need more integrity or fancier relationships later, implement that later, not now.

完成了。如果您以后需要更多的完整性或更高級的關系,請稍后實現,而不是現在。

For deployments, just make sure the file is symlinked to a shared directory to survive application upgrades.

對於部署,只需確保文件與共享目錄相關聯,以在應用程序升級中存活。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2008/12/01/72f6eb77c3806c347176461d6d8d96db.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com