正确的模型数据结构? (我的第一个Rails应用程序) [英] Correct Model Data Structure? (My 1st Rails App)

查看:149
本文介绍了正确的模型数据结构? (我的第一个Rails应用程序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将要建立我的第一个Ruby on Rails应用程序(首先是xhtml和css之外的任何东西),我正在寻找一些关于我的模型结构的有用的反馈。

I'm about to build my first Ruby on Rails application (and first anything beyond xhtml and css), and I'm looking for some helpful feedback on my model structure.

我已经包含一个模型来帮助视觉化应用程序。 / a>

我确信我需要的唯一型号是:

1. 电影模型(作为主要模型,命名为电影,URL的外观将如sitename.com/movies/1-Willy-Wonka)

2.一个用户模型

The only models I'm sure I need so far are:
1. a movie model (to serve as the main model, named movie so URL's will look like, "sitename.com/movies/1-Willy-Wonka") and
2. a user model

除了这些,我不知道该怎么做别人;像拍摄地点,演员,最重要的是像willy wonka和johnny depp这样的属性。

Other than those, I don't know what to make the others; like 'filming locations', 'actors', and most importantly, the attributes like 'willy wonka' and 'johnny depp'.

我应该使所有这些模型,甚至属性?如果是这样,是否可能,并建议让人们将模型创建为属性?

Should I make all of those models, even the attributes? If so, is it possible, and recommended, to let people create models as attributes?

我尝试将应用视为由表单创建并存储在数据库中的数据,使用该pov,我无法决定如何将属性(角色=> actor)连接到电影列表中的部分(演员,拍摄位置,艺术品等);同时允许属性本身是相关属性/电影/甚至部分的超链接:点击史蒂芬·斯皮尔伯格将带您到一个带有电影的页面,他的导演,制作,写作等。

I try to see the app as data created by forms and stored in databases, and with that pov I can't decide how to connect attributes (character <=> actor) to sections (actors, filming locations, artwork, etc.) inside a movie listing; while allowing the attributes themselves to be hyperlinks to related attributes/movies/even sections: clicking on 'steven speilberg' would bring you to a page with movies he's acted in, films he's directed, produced, written, etc.

想法?

推荐答案

我建议先清除数据模型。然后,您可以从每个数据库表的一个rails模型开始。这并不总是这样,但这是一个合理的开始。

I would recommend getting your data model clear first. You can then start with one rails model per database table. This is not always the case, but it's a reasonable place to start.

我们专注于电影和演员:

Let's focus on the movies and actors:

一些假设:


  • 电影可以与其他实体有多对多的关系

  • 您要将角色信息存储在演员和电影之间的关系中。

然后,您可以对关系建模像这样:

You might then model your relationships like this:

# movies.rb
class Movie < ActiveRecord::Base
  has_many :roles
  has_many :actors, :through => :roles
end

# actor.rb
class Actor < ActiveRecord::Base
  has_many :roles
  has_many :movies, :through => :roles
end

通常,您可以依赖Rails的魔法来处理联接模型没有创造自己。在这种情况下,我们要将字符信息存储为连接模型的属性,因此我们明确地创建它。

Normally, you can rely on Rails' magic to handle the join model without creating one yourself. In this case we want to store the character information as attributes of the join model, so we create it explicitly.

# role.rb
class Role < ActiveRecord::Base
  belongs_to :movie
  belongs_to :actor
end

这篇关于正确的模型数据结构? (我的第一个Rails应用程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆