如何最有效地在钢轨一个地址多个模型相关联? [英] How best to associate an Address to multiple models in rails?

查看:96
本文介绍了如何最有效地在钢轨一个地址多个模型相关联?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这对SO问题似乎与我的问题,但我不知道我的问题是由应答。

这是地址可以属于多于一个的模型(用户配置和事件) 什么是实现这个?_爱的正确方法 基本表:

  user_profiles(ID)
事件(id)的
 

有关实施的地址选项的表:

  1. 地址(ID,user_profile_id,EVENT_ID)
    这种做法似乎因为如果明天的地址必须属于一个多模式,我要补充一点,id字段是缺憾。
    另外,我还不知道,但增加了新的ID字段可能会导致某些code 打破呢?

  2. 地址(ID,model_type,model_id)
    这是多态的,正确的。我不知道为什么,但我觉得警惕这种莫名其妙?

  3. 一些其他的方式来做到这一点?

注意的:

我可以做的表是这样的,我想:

  user_profiles(ID,ADDRESS_ID)
事件(ID,ADDRESS_ID)
 

不过,这意味着同样的 ADDRESS_ID 可以属于不同的车型。 我想这不应该是这样的,因为说,例如需要更改地址的事件,但它不应该影响到的USER_PROFILE 地址。 因此,这将是这样的(我认为这是错误的):

  @ current_user_profile.address = some_new_address
#这个会改变的地址都USER_PROFILE *和*事件
@ current_user_profile.save
 

解决方案

一种方法是标准的Rails多态性:

 类地址
  belongs_to的:寻址,多态=>真正
结束

类用户配置
  HAS_ONE地址:作为=> :寻址
结束

类事件
  HAS_ONE地址:作为=> :寻址
结束
 

这可能是唠叨的感觉,你对这个是你不能创建使用Rails风格的多态关系的分贝级约束。另一种方法(丹泽在企业的Rails 建议)是喜欢你的#1选项,在这里确实创造一个单独的id字段为每种类型的关系。但这将未使用的领域,但它也允许限制。我可以看到这两个参数,但Rails社区一直使用AR多态性一段时间显然是很好的成功。我用它毫不犹豫地。但如果你的错误,你可以用泽的做法。这是一个很多工作。 :)

编辑:@ Slick86,迁移看起来是这样的:

 类CreateAddresses< ActiveRecord的::迁移
  高清变化
    CREATE_TABLE:地址做| T |
      t.integer:addressable_id
      t.string:addressable_type
    结束
  结束
结束
 

This question on SO appears to be related to my question, but I'm not sure my question is answered by that.

An address can belong to more than one model (UserProfile and Event) What's the correct way to implement this?

The basic tables:

user_profiles(id)
events(id)

Options for implementing the addresses table:

  1. addresses(id,user_profile_id,event_id)
    This approach seems to be kludgy since if tomorrow the address needs to belong to one more model, I have to add that id field.
    Also, I don't know yet, but adding a new id field may cause some code to break as well ?

  2. addresses(id,model_type,model_id)
    This is polymorphic, right. I'm don't know why, but I feel wary of this somehow?

  3. Some other way to do this?

Note:

I could have made the tables like this, I suppose:

user_profiles(id,address_id)
events(id,address_id)

But, this means that the same address_id can belong to different models. I suppose it should not be that way, because say for example that the address for the event needs to be changed, but it should not affect the address of the user_profile. So that would be something like this (which I think is wrong):

@current_user_profile.address = some_new_address
#this would have changed the address for both the user_profile *and* the event
@current_user_profile.save 

解决方案

One way would be standard Rails polymorphism:

class Address
  belongs_to :addressable, :polymorphic => true
end

class UserProfile
  has_one address, :as => :addressable
end

class Event
  has_one address, :as => :addressable
end

It could be the nagging feeling you have about this is that you can't create a db-level constraint with Rails-style polymorphic relationships. The alternative (suggested by Dan Chak in Enterprise Rails) is like your #1 option, where you do indeed create a separate id field for each type of relationship. This does leave unused fields, but it also allows for constraints. I can see the argument for both, but the Rails community has been using AR polymorphism for some time with apparently good success. I use it without hesitation. But if that bugs you, you can use Chak's approach. It's a lot more work. : )

EDIT: @Slick86, migration would look something like:

class CreateAddresses < ActiveRecord::Migration
  def change
    create_table :addresses do |t|
      t.integer :addressable_id
      t.string :addressable_type
    end
  end
end

这篇关于如何最有效地在钢轨一个地址多个模型相关联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆