设置州和城市 Rails 模型、ActiveRecord 关联和表单
Setting up State and Cities Rails Models, ActiveRecord Associations and Form
我想知道实现以下内容的最佳方法:
用户可以创建一个 Post
,每个 Post 都与一个美国城市和州相关联。
我想要一个表单,其中有一个下拉框,用户可以 select State
和 select 相关的 City
例如:[好莱坞] [加利福尼亚].
我已经设置了 State
和 City
模型。
国家模式:
# == Schema Information
#
# Table name: states
#
# id :integer not null, primary key
# name :string default(""), not null
# short :string default(""), not null
# created_at :datetime not null
# updated_at :datetime not null
#
class State < ApplicationRecord
has_many :cities
end
城市模式:
# == Schema Information
#
# Table name: cities
#
# id :integer not null, primary key
# name :string
# state_id :integer
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_cities_on_state_id (state_id)
#
class City < ApplicationRecord
belongs_to :state
end
使用 Post
模型
# == Schema Information
#
# Table name: posts
#
# id :integer not null, primary key
# title :string default(""), not null
# body :string default(""), not null
class Post < ApplicationRecord
end
对于 Post
模型,我想我会 belongs_to
和 has_many
与 references/foreign 键设置的关联,例如:
class Post < ApplicationRecord
belongs_to :city
belongs_to :state
end
class City < ApplicationRecord
belongs_to :state
has_many :posts
end
class State < ApplicationRecord
has_many :cities
has_many :posts
end
该视图将使用 form_for
,城市使用 grouped_collection
,州使用 collection
,但我认为这不是一个好的实现,因为:
- 选择
city
已经作为与状态的关联。让用户 select 两个模型选项似乎是多余的?
- 视图中的下拉框将加载 60000 条城市记录,这会降低浏览器速度。
我想知道 Polymorphic
关联是否适合我的用例,以及是否有人可以向我推荐正确的方向。
我的想法: 我在想用户 select 在下拉框中 State
的东西,例如。 California,但是在 text field
进入城市,如果 Cities
table 中不存在城市,则创建它,否则它将 link 到它?
感谢您的帮助。
你要的是has_one through:
association。这种间接关系告诉 rails 通过另一个关联加入并消除了对重复外键的需要。
class Post < ApplicationRecord
belongs_to :city
has_one :state, through: :city
end
要创建完整的层次结构,您可以这样做:
class Post < ApplicationRecord
belongs_to :city
has_one :state, through: :city
has_one :country, through: :state
end
class City < ApplicationRecord
belongs_to :state
has_one :country, through: :state
has_many :posts
end
class State
belongs_to :country
has_many :cities
has_many :posts, through: :cities
end
class Country < ApplicationRecord
has_many :states
has_many :cities, through: :states
has_many :posts, through: :cities
end
Polymorphic associations 完全是一个非常不同的东西——当一个关联可以针对不同的模型时使用它。在此示例中,Comment
可以属于 Post
或 Page
。
class Comment < ApplicationRecord
belongs_to :commentable, polymorphic: true
end
class Post < ApplicationRecord
# this tells AR to look at the `commentable` association on
# Comment.
has_many :comments, as: :commentable
end
class Page < ApplicationRecord
has_many :comments, as: :commentable
end
我想知道实现以下内容的最佳方法:
用户可以创建一个
Post
,每个 Post 都与一个美国城市和州相关联。我想要一个表单,其中有一个下拉框,用户可以 select
State
和 select 相关的City
例如:[好莱坞] [加利福尼亚].
我已经设置了 State
和 City
模型。
国家模式:
# == Schema Information
#
# Table name: states
#
# id :integer not null, primary key
# name :string default(""), not null
# short :string default(""), not null
# created_at :datetime not null
# updated_at :datetime not null
#
class State < ApplicationRecord
has_many :cities
end
城市模式:
# == Schema Information
#
# Table name: cities
#
# id :integer not null, primary key
# name :string
# state_id :integer
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_cities_on_state_id (state_id)
#
class City < ApplicationRecord
belongs_to :state
end
使用 Post
模型
# == Schema Information
#
# Table name: posts
#
# id :integer not null, primary key
# title :string default(""), not null
# body :string default(""), not null
class Post < ApplicationRecord
end
对于 Post
模型,我想我会 belongs_to
和 has_many
与 references/foreign 键设置的关联,例如:
class Post < ApplicationRecord
belongs_to :city
belongs_to :state
end
class City < ApplicationRecord
belongs_to :state
has_many :posts
end
class State < ApplicationRecord
has_many :cities
has_many :posts
end
该视图将使用 form_for
,城市使用 grouped_collection
,州使用 collection
,但我认为这不是一个好的实现,因为:
- 选择
city
已经作为与状态的关联。让用户 select 两个模型选项似乎是多余的? - 视图中的下拉框将加载 60000 条城市记录,这会降低浏览器速度。
我想知道 Polymorphic
关联是否适合我的用例,以及是否有人可以向我推荐正确的方向。
我的想法: 我在想用户 select 在下拉框中 State
的东西,例如。 California,但是在 text field
进入城市,如果 Cities
table 中不存在城市,则创建它,否则它将 link 到它?
感谢您的帮助。
你要的是has_one through:
association。这种间接关系告诉 rails 通过另一个关联加入并消除了对重复外键的需要。
class Post < ApplicationRecord
belongs_to :city
has_one :state, through: :city
end
要创建完整的层次结构,您可以这样做:
class Post < ApplicationRecord
belongs_to :city
has_one :state, through: :city
has_one :country, through: :state
end
class City < ApplicationRecord
belongs_to :state
has_one :country, through: :state
has_many :posts
end
class State
belongs_to :country
has_many :cities
has_many :posts, through: :cities
end
class Country < ApplicationRecord
has_many :states
has_many :cities, through: :states
has_many :posts, through: :cities
end
Polymorphic associations 完全是一个非常不同的东西——当一个关联可以针对不同的模型时使用它。在此示例中,Comment
可以属于 Post
或 Page
。
class Comment < ApplicationRecord
belongs_to :commentable, polymorphic: true
end
class Post < ApplicationRecord
# this tells AR to look at the `commentable` association on
# Comment.
has_many :comments, as: :commentable
end
class Page < ApplicationRecord
has_many :comments, as: :commentable
end