Ruby on Rails 3 - Rails 新手 - 了解评级应用程序的关系

发布于 2024-11-01 22:24:33 字数 377 浏览 2 评论 0原文

我决定使用 Rails 3 构建我的最新站点。这是我第一次使用 Rails,并且希望获得社区关于如何执行以下场景的意见。

我创建了以下模型:项目、评级、用户

我希望应用程序的工作方式为:

1) 项目有很多评级
2) 用户可以提交多个评分 - 每个项目只有一个用户提交评分
3)特定评级只能有一个项目和一个用户

基于此我希望能够:

1)显示项目的所有评级
2) 显示特定用户评分的所有项目

看起来很简单。非常感谢任何帮助或指导。

I've decided to build my newest site using Rails 3. This is my first experience with Rails and wanted to get the communities opinion on how to do the following scenario.

I have the following models created: Item, Rating, User

I would like the app to work as:

1) Item has many Ratings
2) User can submit many Ratings - Only one user submitted rating per item
3) Specific rating can can only have one Item and one User

Based on this I want to be able to:

1) Show all ratings for an item
2) Show all items rated by a particular user

Seems simple enough. Any help or direction is appreciated greatly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞美少年 2024-11-08 22:24:33

我将使用多态关联:

# file: app/models/item.rb
class Item < ActiveRecord::Base
  has_many :ratings, :as => :rateable
end

# file: app/models/user.rb
class User < ActiveRecord::Base
  has_many :ratings
  has_many :rated_items, :through => :ratings, :source => :rateable, :source_type => "Item"
end

# file: app/models/rating.rb
class Rating < ActiveRecord::Base
  belongs_to :user
  belongs_to :rateable, :polymorphic => true
  validates_uniqueness_of :user_id, :scope => :rateable
end

这样您的用户可以对不同的项目进行评分,但每个项目只能评分一次。

要实现此目的,您需要在 ratings 表中添加 rateable_idrateable_type 字段。另外,当然还有 user_idrating_score 之类的。

最大的优点是您可以根据需要添加任意数量的rateables。例如,如果您想对 Song 模型进行评分,那么您可以简单地实现它,如下所示:

# file: app/models/song.rb
class Song < ActiveRecord::Base
  has_many :ratings, :as => :rateable
end

要显示 Item 的评分:item. ratings< /代码>
要显示用户评分的所有项目:user.lated_items

PS我的英语语法不好,所以如果拼写错误rateable请纠正我< br>
PPS 这个实现未经测试,是我直接从脑子里写出来的,所以不能保证它能 100% 工作:)

祝你好运!

I would use Polymorphic association:

# file: app/models/item.rb
class Item < ActiveRecord::Base
  has_many :ratings, :as => :rateable
end

# file: app/models/user.rb
class User < ActiveRecord::Base
  has_many :ratings
  has_many :rated_items, :through => :ratings, :source => :rateable, :source_type => "Item"
end

# file: app/models/rating.rb
class Rating < ActiveRecord::Base
  belongs_to :user
  belongs_to :rateable, :polymorphic => true
  validates_uniqueness_of :user_id, :scope => :rateable
end

With this your User can rate different Items, but only once per Item.

To implement this you'd need rateable_id and rateable_type fields in your ratings table. Plus, off course, user_id and rating_score or something.

The great advantage is that you are able to add as much rateables as you wish. If, for example, you want to rate a Song model, then you can simply implement it like this:

# file: app/models/song.rb
class Song < ActiveRecord::Base
  has_many :ratings, :as => :rateable
end

To show ratings for an Item: item.ratings
To show all items rated by a User: user.rated_items

P.S. I am not good in English grammar, so correct me if have misspelled rateable
P.P.S. this implementation is untested, I wrote it straight out of my head, so there's no guarantee it will work 100% :)

Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文