0
我建立使用Rails 4Rails的:多態關聯
的應用程序我有一個地址模型和手機型號。它們中的每一個都被定義爲與多個其他模型的多態性,以便它們可以在整個應用程序中使用 - 例如,用戶具有電話號碼,但公司也是如此。他們的數字可能不同。該公司有一個地址,它可以被用戶用作默認地址,或者他們可以添加另一個地址作爲自己的地址。
在我的代碼,我有一個地址模型:
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
我不明白錯誤的性質。這個結構缺失了什麼?