1
class User < ActiveRecord::Base
  has_one :location, :dependent => :destroy, :as => :locatable
  has_one :ideal_location, :dependent => :destroy, :as => :locatable
  has_one :birthplace, :dependent => :destroy, :as => :locatable
end

class Location < ActiveRecord::Base
  belongs_to :locatable, :polymorphic => true
end

class IdealLocation < ActiveRecord::Base
end

class Birthplace < ActiveRecord::Base
end

在这种情况下,我真的看不出有任何理由有子类。位置对象的行为是相同的,唯一的一点是使关联更容易。我还希望将数据存储为 int 而不是字符串,因为它允许数据库索引更小。

我设想如下内容,但我无法完成这个想法:

class User < ActiveRecord::Base
  LOCATION_TYPES = { :location => 1, :ideal_location => 2, :birthplace => 3 }

  has_one :location, :conditions => ["type = ?", LOCATION_TYPES[:location]], :dependent => :destroy, :as => :locatable
  has_one :ideal_location, :conditions => ["type = ?", LOCATION_TYPES[:ideal_location]], :dependent => :destroy, :as => :locatable
  has_one :birthplace, :conditions => ["type = ?", LOCATION_TYPES[:birthplace]], :dependent => :destroy, :as => :locatable
end

class Location < ActiveRecord::Base
  belongs_to :locatable, :polymorphic => true
end

使用此代码,以下代码将失败,基本上使其无用:

user = User.first
location = user.build_location
location.city = "Cincinnati"
location.state = "Ohio"
location.save!

location.type # => nil

这很明显,因为无法将 has_one 声明中的 :conditions 选项转换为等于 1 的类型。

我可以将 id 嵌入到视图中出现这些字段的任何位置,但这似乎也是错误的:

<%= f.hidden_field :type, LOCATION_TYPES[:location] %>

有没有办法避免额外的子类或使 LOCATION_TYPES 方法起作用?

在我们的特定情况下,应用程序非常具有位置感知能力,并且对象可以具有许多不同类型的位置。我只是因为不想要所有这些子类而感到奇怪吗?

感谢您提出任何建议,如果您愿意,请告诉我我疯了,但是您是否希望看到 10 多个不同的位置模型在应用程序/模型周围浮动?

4

5 回答 5

2

为什么不使用 named_scopes?

就像是:

class User
  has_many :locations
end

class Location
  named_scope :ideal, :conditions => "type = 'ideal'"
  named_scope :birthplace, :conditions => "type = 'birthplace" # or whatever
end

然后在您的代码中:

user.locations.ideal => # list of ideal locations
user.locations.birthplace => # list of birthplace locations

我认为,您仍然必须在创​​建时设置类型。

于 2010-02-18T05:33:58.780 回答
1

据我所知,一个位置就是一个位置就是一个位置。您所指的不同“子类”(IdealLocation,Birthplace)似乎只是在描述该位置与特定用户的关系。如果我弄错了那部分,请阻止我。

知道了这一点,我可以看到两种解决方案。

第一个是将位置视为值对象而不是实体。(有关术语的更多信息:值与实体对象(领域驱动设计))。在上面的示例中,您似乎将位置设置为“Cincinnati, OH”,而不是从数据库中查找“Cincinnati, OH”对象。在这种情况下,如果 Cincinnati 存在许多不同的用户,那么您的数据库中将有相同数量的“Cincinnati, OH”位置,尽管只有一个 Cincinnati, OH。对我来说,这清楚地表明您正在使用值对象,而不是实体。

这个解决方案看起来如何?可能使用这样的简单 Location 对象:

class Location
  attr_accessor :city, :state

  def initialize(options={})
    @city = options[:city]
    @state = options[:state]
  end
end

class User < ActiveRecord::Base
  serialize :location
  serialize :ideal_location
  serialize :birthplace
end

@user.ideal_location = Location.new(:city => "Cincinnati", :state => "OH")
@user.birthplace = Location.new(:city => "Detroit", :state => "MI")
@user.save!

@user.ideal_location.state # => "OH"

我可以看到的另一个解决方案是使用您现有的 Location ActiveRecord 模型,但只需使用与 User 的关系来定义关系“类型”,如下所示:

class User < ActiveRecord::Base
  belongs_to :location, :dependent => :destroy
  belongs_to :ideal_location, :class_name => "Location", :dependent => :destroy
  belongs_to :birthplace, :class_name => "Location", :dependent => :destroy
end

class Location < ActiveRecord::Base
end

完成这项工作所需要做的就是在 User 模型中包含 location_id、ideal_location_id 和birthplace_id 属性。

于 2010-02-18T17:22:16.037 回答
0

尝试添加 before_save 钩子

class Location
  def before_save
    self.type = 1
  end
end

同样适用于其他类型的位置

于 2010-02-18T02:50:25.920 回答
0

您可以使用模块封装 Location 对象的行为,并使用一些宏来创建关系:

has_one <location_class>,: conditions => [ "type =?" LOCATION_TYPES [: location]],: dependent =>: destroy,: as =>: locatable

你可以在你的模块中使用这样的东西:

module Orders
  def self.included(base)
    base.extend(ClassMethods)
  end

  module ClassMethods
    def some_class_method(param)
    end

    def some_other_class_method(param)
    end

    module InstanceMethods
      def some_instance_method
      end
    end
  end
end

Rails 指南:将行为作为方法添加到活动记录

于 2010-02-18T02:51:55.880 回答
0

也许我在这里遗漏了一些重要的东西,但我认为你可以这样命名你的关系:

class User < ActiveRecord::Base

  has_one :location, :dependent => :destroy
  #ideal_location_id
  has_one :ideal_location, :class_name => "Location", :dependent => :destroy
  #birthplace_id
  has_one :birthplace, :class_name => "Location", :dependent => :destroy

end

class Location < ActiveRecord::Base
  belongs_to :user # user_id
end
于 2010-02-18T20:15:25.873 回答