Rails:多态关联

梅尔

我正在使用Rails 4构建应用程序。

我有一个地址模型和一个电话模型。它们中的每一个都被定义为带有多种其他模型的多形模型,因此它们可以在整个应用程序中使用-例如,用户拥有电话号码,公司也可以使用。他们的人数可能有所不同。该公司有一个地址,用户可以将其用作默认地址,或者他们可以添加另一个地址作为自己的地址。

在我的代码中,我有一个地址模型为:

class Address < ActiveRecord::Base

  geocoded_by :full_address_map   # can also be an IP address


  # --------------- associations

  belongs_to :addressable, :polymorphic => true

  # --------------- scopes

  # --------------- validations

  validates_presence_of :unit, :street, :zip, :country 


  # --------------- class methods

  def first_line
    [unit, street].join(' ')
  end

  def middle_line
    if self.building.present? 
    end
  end

  def last_line
    [city, region, zip].join('   ')
  end

  def country_name
    self.country = ISO3166::Country[country]
    country.translations[I18n.locale.to_s] || country.name
  end

  def address_without_country
    [self.first_line, middle_line, last_line].compact.join(" ")
  end

  def full_address_map
    [self.first_line, middle_line, last_line, country_name.upcase].compact.join("<br>").html_safe
  end

  def full_address_formal
    [self.first_line, middle_line, last_line, country_name].compact.join(" ").html_safe
  end


  # --------------- callbacks

  after_validation :geocode#, if  self.full_address.changed? 

  # --------------- instance methods

  # --------------- private methods

  protected

end

在我的地址_form部分中,我有供用户填写的表单。

在我的组织模型中,我有:

class Organisation < ActiveRecord::Base

  # --------------- associations

    has_one :user # the user that is the representative 
    has_many :profiles # the users who have roles within the org


    has_many :addresses, :as_addressable
    has_many :phones, :as_phoneable


  # --------------- scopes



  # --------------- validations

  # --------------- class methods


  def address
    self.address.full_address_formal
  end


  # --------------- callbacks

  # --------------- instance methods

  # --------------- private methods


end

在我的组织控制者(新动作)中,我有:

def new
    @organisation = Organisation.new
    @organisation.build_address
end

当我尝试此操作时,出现以下错误:

NoMethodError at /organisations/new
undefined method `arity' for :as_addressable:Symbol

在我的地址表中,我有:

t.integer  "addressable_id"
t.string   "addressable_type"
add_index "addresses", ["addressable_type", "addressable_id"], name: "index_addresses_on_addressable_type_and_addressable_id", unique: true, using: :btree

我不了解错误的性质。这种结构缺少什么?

梅桑贾

多态关联as在其声明中需要该选项。

has_many :addresses, as: :addressable
has_many :phones, as: :phoneable

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Rails多态关联参考

来自分类Dev

Rails:包含多态关联

来自分类Dev

Rails逆多态关联

来自分类Dev

多态关联-Rails

来自分类Dev

Rails 5.2 多态关联与多态条件

来自分类Dev

Rails:属于多态关联+条件

来自分类Dev

测试Rails中的多态关联

来自分类Dev

Ruby on Rails多态关联-案例

来自分类Dev

测试Rails中的多态关联

来自分类Dev

Rails 多态关联多模型

来自分类Dev

Rails多态关联自指关联

来自分类Dev

Rails 4-多态关联-嵌套属性

来自分类Dev

尝试避免Rails中的多态关联

来自分类Dev

Rails处理多态关联的方式是什么?

来自分类Dev

Rails最佳实践,使用多态关联

来自分类Dev

Rails 4-Railscast#154多态关联

来自分类Dev

rails-多态关联或单表继承

来自分类Dev

Rails多态关联不保存ID

来自分类Dev

Rails别名,用于多态关联上的属性

来自分类Dev

Rails 多态关联,获取父级评论

来自分类Dev

Rails 统计多态关联中的记录数

来自分类Dev

多态关联中的查询 - Rails 5.1

来自分类Dev

Rails has_one 与多态关联

来自分类Dev

Rails-多态关联-处理可选关联的最佳做法

来自分类Dev

Rails 4多态关联和关注点

来自分类Dev

Rails:多态关联,取决于类型的不同选项?

来自分类Dev

Rails多态关联表示未定义的方法

来自分类Dev

等效于Entity Framework中Ruby on Rails的多态关联

来自分类Dev

Rails中同一模型上的多个多态关联