简体   繁体   English

Rails活动记录协会

[英]Rails Active Record Associations

I've been reading and re-reading the Rails associations guide: 我一直在阅读和重新阅读Rails关联指南:

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

This were close but not quite the same: 这很接近,但并不完全相同:

Ruby on rails active record associations Ruby on Rails活动记录关联

I'm not sure how to setup the following scenario. 我不确定如何设置以下方案。

Events has a status of either pending, open, or close. 事件的状态为未决,打开或关闭。

I thought this would be simple enough to just have: 我认为这很简单,只需拥有:

event has_one status
status belongs_to event

But this really isn't a one-to-one relationship since a status can belong to many events. 但这实际上不是一对一的关系,因为状态可以属于许多事件。

So then I thought I would do something like: 所以后来我想我会做类似的事情:

status has_many events
event belongs_to status

But this seems funny, because a status doesn't own an event. 但这似乎很有趣,因为状态并不拥有事件。 An event owns a status, right? 事件拥有状态,对吗?

I had tried using enumerations and not have a status model. 我曾尝试使用枚举,但没有状态模型。 But that got tricky since it seems like ActiveRecord doesn't really support enumerations. 但这很棘手,因为ActiveRecord似乎并不真正支持枚举。 I also figured that having a separate model might be good in case someone wants to expand on the number of options for status, like adding 'awaiting approval' or something. 我还认为,如果有人想扩展身份选项的数量,例如添加“等待批准”之类的东西,那么拥有一个单独的模型可能会很好。

This post suggests that my latter setup is okay, even though it reads funny: 这篇文章表明我的后面的设置还可以,即使它看起来很有趣:

Really easy Rails Active-Record Associations question 真的很容易的Rails Active-Record Associations问题

But I'm just wondering if I'm not aware of a better Ruby/Rails way of handling this simple scenario. 但是我只是想知道我是否不知道一种更好的Ruby / Rails处理这种简单情况的方法。

Thanks in advance! 提前致谢!

Doing this as an active record association is overkill. 作为一个活跃的唱片协会这样做是过分的。 Think about it, create a whole table just to store 3 values in it that will never change? 想一想,创建一个仅在其中存储3个永不改变的值的表格吗?

What you really need is an enum. 您真正需要的是一个枚举。 But of course, ruby doesn't have an enum. 但是,当然,红宝石没有枚举。

Luckily, you can fake it. 幸运的是,您可以伪造它。

module StatusCodes
  pending = 0
  open = 1
  closed = 2
end

Then you can do something like this 然后你可以做这样的事情

if @event.status == StatusCodes::open
  # do something
end

It's a much simpler solution and your code stays very readable. 这是一个简单得多的解决方案,您的代码保持了很高的可读性。

Ignore that voice in your head: you're doing it fine. 忽略您头脑中的声音:您做得很好。 The real important of which model has belongs_to is where the foreign key is stored. 哪个模型具有belongs_to的真正重要之处在于存储外键的位置。 It's clear in this example that the foreign key should be stored in the Event model, which means it should belongs_to :status . 在此示例中很明显,外键应该存储在Event模型中,这意味着它应该belongs_to :status

I also agree with the other posts, though - if you have got a small and fixed number of potential Status records, consider creating an constant hash to store them instead of creating a whole database table for them. 不过,我也同意其他文章-如果您的潜在Status记录数量少且固定,请考虑创建一个常量散列来存储它们,而不是为它们创建整个数据库表。

Why not add a status column to Event (as an integer), and have something like this: 为什么不将status列添加到Event (作为整数),并具有以下内容:

class Event < ActiveRecord::Base
  STATUS_TYPES = {1 => "active", 2 => "inactive", 3 => "closed"}

  def status
    STATUS_TYPES[self[:status]]
  end

  def status=(new_status)
    new_status = STATUS_TYPES.invert[new_status] if new_status.class == "String"
    self[:status] = new_status
  end
end

您可能要考虑使用state_machine和一个简单的字符串列来实现状态,而不是使用关联或手动枚举。

You are wrong. 你错了。

If you do 如果你这样做

Event
 has_one :status

Status
 belongs_to :event

Rails will make sure it is a one-to-one association, so status will only belong to one event Rails将确保它是一对一的关联,因此状态将仅属于一个事件

I'm pretty sure this is what happens if you try to assign a event status to a different event 我很确定,如果您尝试将事件状态分配给其他事件,将会发生这种情况

e1 = Event.first
status = e1.status

e2 = Event.new
e2.status = status
e2.save

Event.first.status #=> nil

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM