我正在尝试对生成的模型字符串进行搜索,该字符串由 has_one 关联形成。这是我的模型:
街道.rb
class Street < ActiveRecord::Base
has_many :houses
validates :street_type, presence: true
validates :name, presence: true, uniqueness: {:scope => :street_type, :case_sensitive => false }
def text
street_type + " " + name
end
end
房子.rb
class House < ActiveRecord::Base
belongs_to :street
has_many :apartments
validates :street_id, presence: true
validates :number, presence: true
def text
street.text + ", " + number
end
end
公寓.rb
class Apartment < ActiveRecord::Base
belongs_to :house
has_one :street, through => :house
has_many :accounts
validates :house_id, presence: true
validates :number, presence: true, uniqueness: {:scope => :house_id, :case_sensitive => false}
def text
house.text + ", ap. " + number
end
end
账户.rb
class Account < ActiveRecord::Base
belongs_to :appartment
has_one :house, :through => :appartment
belongs_to :resident
validates :appartment_id, presence: true
validates :resident_id, presence: true, uniqueness: {:scope => :appartment_id, :case_sensitive => false}
def text
uniq_id + " (" + appartment.text + ")"
end
end
我必须通过匹配一些地址字符串来搜索所有帐户。我不知道该怎么做。
- 示例第一个帐户文本“123-1123-122(Regent street, 51, ap. 3)”
- 示例第二个帐户文本“123-1123-155 (Regent street, 51, ap. 6)”
- 示例搜索文本“Regent street, 51”
有人可以帮助我了解如何做到这一点吗?