Rails 中模型只有一个实例

发布于 2024-08-03 10:42:34 字数 222 浏览 10 评论 0原文

我正在开发我的第一个 Rails 应用程序。它需要存储有关将出现在每个页面上的单个广告的一些信息。管理员只需要能够设置 URL、标题和图像。显然,我只需要该广告对象的一个​​实例。

我创建了一个继承自 ActiveRecod::Base 的模型,但这似乎是错误的做法,因为它被配置为在数据库表中保存多个广告。

对我来说最好的方法是什么?模型和控制器应该是什么样子?

提前致谢, 阿维

I'm working on my first Rails app. It needs to store some information about a single advertisement that will appear on every page. The admin just needs to be able to set the URL, Title, and an image. Obviously, I only need one instance of this ad object.

I created a model that inherits from ActiveRecod::Base, but that seems like the wrong thing to do, since it is configured to save multiple ads in a database table.

What's the best way for me to do this? What should the model and controller look like?

Thanks in advance,
Avi

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

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

发布评论

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

评论(4

多情癖 2024-08-10 10:42:34

更好的方法是添加一个验证来检查一个记录条目是否已存在。

在你的模型内部:

validate :check_record, on: :create #please not that validate in this case is singular

def check_record
 if Ad.all.count === 1
   errors[:base] << "You can only have one active advertisement"
 end
end

A better way would be adding a validation that checks if one record entry already exists.

Inside your model:

validate :check_record, on: :create #please not that validate in this case is singular

def check_record
 if Ad.all.count === 1
   errors[:base] << "You can only have one active advertisement"
 end
end
写给空气的情书 2024-08-10 10:42:34

好吧,如果您要将信息(URL、标题、图像)存储在数据库中,我认为从 AR 继承它是正确的做法。如果他们要通过任何类型的前端来完成此操作,我认为这是您的最佳选择。

为什么不允许多个广告,但一次只能发布一个?这样,您还可以了解广告的历史记录 - 我不知道这部分有多重要,但它可能会证明很有趣。

Well, if you are going to store the information (URL, Title, image) in the database, I think having it inheriting from AR is the right thing to do. If they are going to do this through any sort of front-end, I think this is your best option.

Why not allow for multiple advertisements, but only one of them can be published at a time? That way, you can also have a history of the advertisements—I don't know how important that part is, but it might prove interesting down the line.

单身狗的梦 2024-08-10 10:42:34

我建议您继续使用 ActiveRecord,但添加一个布尔属性来确定众多广告记录中哪一个是活动记录。我在下面的示例中将该字段称为活动字段。

然后,您可以在模型中验证最多一条记录处于活动状态。在以下任一条件下,活动属性的验证应成功

  1. 活动属性设置为 false
  2. 有 0 条活动值为 true 的记录。
  3. 当前记录已在数据库中将该属性设置为 true。

以下课程应该可以满足您的需求

class Ad < ActiveRecord::Base

  named_scope :has_active, :conditions => {:active => true}

  def validate
    errors.add_to_base "You can only have one active advertisement" 
        unless self.active_flag_valid?
  end

  def active_flag_valid?
    self.active == false || 
    Ad.has_active.size == 0 || 
    ( Ad.has_active.size == 1 && !self.active_changed?)
  end
end

I would suggest that you continue using ActiveRecord but that you add a boolean attribute that determines which of the many advertisement records is the active one. I have called this field active within the following example.

You can then validate within the model that a maximum of one record is active. The validation of the active attribute should succeed under any of the following conditions

  1. The active attribute is set to false
  2. There are 0 records with an active value of true.
  3. The current record already has the attribute set to true in the database.

The following class should meet your needs

class Ad < ActiveRecord::Base

  named_scope :has_active, :conditions => {:active => true}

  def validate
    errors.add_to_base "You can only have one active advertisement" 
        unless self.active_flag_valid?
  end

  def active_flag_valid?
    self.active == false || 
    Ad.has_active.size == 0 || 
    ( Ad.has_active.size == 1 && !self.active_changed?)
  end
end
川水往事 2024-08-10 10:42:34

我同意。如果您完全确定它是静态的,那么它甚至不需要存储在数据库中。如果它确实发生变化,Rails 会免费为您提供created_at 和updated_at,因此获取最近创建/更新的内容是显示当前内容的一种简单方法。

I agree. If you're completely sure it's static, then it doesn't even need to be stored in the database. If it does change, Rails will give you created_at and updated_at pretty much for free, so getting the most recently created / updated is one simple way to approach displaying what's current.

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