这些 Rails 模型应该如何关联?
How should these Rails Models be associated?
我想得太多了,现在我才感到困惑。
我有一个 Rails 应用程序,其模型如下:用户、站点、教程、教程步骤。
一个用户可以创建很多教程,每个教程都有一个站点,每个教程都有很多教程步骤。
我遇到问题的模型是 Site。
某用户有很多教程,教程中有很多教程步骤。一个教程属于一个用户,有很多教程步骤,最后一个教程步骤属于一个教程。
现在网站应该属于用户和教程,还是只属于教程?
用户
has_many :tutorials
网站
has_many :tutorials
教程
has_many :tutorial_steps
belongs_to :user
belongs_to :site
教程需要站点 ID 和用户 ID 才能正确地将关联映射到用户和站点
教程 Table (... user_id,site_id)
应该是这样的,
user.rb
has_many :tutorials
has_many :tutorial_steps, through: :tutorials
site.rb
has_many :tutorials
tutorial.rb
has_many :tutorial_steps
belongs_to :user
belongs_to :site
tutorial_step.rb
belongs_to :tutorial
has_one :user, through: :tutorial
has_one :site, through: :tutorial
希望对您有所帮助!
据我从你的问题中了解到你可以使用 has_one
关系。
has_one :指定与另一个 class
的一对一关联
您可以与站点和教程建立 has_one 关系
app/models/user.rb
has_many :tutorials,->{ includes :site}
has_many :tutorial_steps, through: :tutorials
包括 eager loading
技术,可让您获取站点以及教程关系
app/models/tutorial.rb
belongs_to :site
belongs_to :user
has_many :tutorial_steps
app/models/site.rb
has_one :tutorial
app/models/tutorial_step.rb
belongs_to :tutorial
我会按如下方式构建关系:
class User < ApplicationRecord
has_many :tutorials
has_many :sites, through: :tutorials
...
end
class Site < ApplicationRecord
has_many :tutorials
has_many :users, through: :tutorials
...
end
class Tutorial < ApplicationRecord
has_many :tutorial_steps
belongs_to :user
belongs_to :site
...
end
class TutorialStep < ApplicationRecord
belongs_to :tutorial
...
end
使 Site
属于 User
将该站点仅绑定到单个用户,这意味着多个用户不能在同一站点上放置教程,您必须重复输入同一个网站能够实现这一点,在我看来这不是很好的模型设计,因为如果在现实生活中多个用户在同一个网站上有不同的教程,你希望能够在你的模型中反映相同的行为和数据。因此,如果您希望能够从 User
引用站点,并从 Site
引用用户,我建议在链接它们的 table 上使用 has many through 关系,即教程 table,就像我在上面展示的那样。
我想得太多了,现在我才感到困惑。
我有一个 Rails 应用程序,其模型如下:用户、站点、教程、教程步骤。
一个用户可以创建很多教程,每个教程都有一个站点,每个教程都有很多教程步骤。
我遇到问题的模型是 Site。
某用户有很多教程,教程中有很多教程步骤。一个教程属于一个用户,有很多教程步骤,最后一个教程步骤属于一个教程。
现在网站应该属于用户和教程,还是只属于教程?
用户
has_many :tutorials
网站
has_many :tutorials
教程
has_many :tutorial_steps
belongs_to :user
belongs_to :site
教程需要站点 ID 和用户 ID 才能正确地将关联映射到用户和站点
教程 Table (... user_id,site_id)
应该是这样的,
user.rb
has_many :tutorials
has_many :tutorial_steps, through: :tutorials
site.rb
has_many :tutorials
tutorial.rb
has_many :tutorial_steps
belongs_to :user
belongs_to :site
tutorial_step.rb
belongs_to :tutorial
has_one :user, through: :tutorial
has_one :site, through: :tutorial
希望对您有所帮助!
据我从你的问题中了解到你可以使用 has_one
关系。
has_one :指定与另一个 class
的一对一关联您可以与站点和教程建立 has_one 关系
app/models/user.rb
has_many :tutorials,->{ includes :site}
has_many :tutorial_steps, through: :tutorials
包括 eager loading
技术,可让您获取站点以及教程关系
app/models/tutorial.rb
belongs_to :site
belongs_to :user
has_many :tutorial_steps
app/models/site.rb
has_one :tutorial
app/models/tutorial_step.rb
belongs_to :tutorial
我会按如下方式构建关系:
class User < ApplicationRecord
has_many :tutorials
has_many :sites, through: :tutorials
...
end
class Site < ApplicationRecord
has_many :tutorials
has_many :users, through: :tutorials
...
end
class Tutorial < ApplicationRecord
has_many :tutorial_steps
belongs_to :user
belongs_to :site
...
end
class TutorialStep < ApplicationRecord
belongs_to :tutorial
...
end
使 Site
属于 User
将该站点仅绑定到单个用户,这意味着多个用户不能在同一站点上放置教程,您必须重复输入同一个网站能够实现这一点,在我看来这不是很好的模型设计,因为如果在现实生活中多个用户在同一个网站上有不同的教程,你希望能够在你的模型中反映相同的行为和数据。因此,如果您希望能够从 User
引用站点,并从 Site
引用用户,我建议在链接它们的 table 上使用 has many through 关系,即教程 table,就像我在上面展示的那样。