您如何评价同一对象的多个属性?

发布于 2024-08-19 12:26:09 字数 124 浏览 5 评论 0原文

我还没有看到这个功能作为插件,并且想知道如何实现它,希望使用 Rails。

我所追求的功能是能够在一页/表格上通过各种属性(例如情节、娱乐性、原创性等)对一个对象(一部电影)进行评级。

有人可以帮忙吗?

I've not seen this feature as a plug in and was wondering how to achieve it, hopefully using rails.

The feature I'm after is the ability to rate one object (a film) by various attributes such as plot, entertainment, originality etc etc on one page/form.

Can anyone help?

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

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

发布评论

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

评论(3

め可乐爱微笑 2024-08-26 12:26:09

我不认为你需要一个插件来做到这一点...你可以使用 AR 执行以下操作

class Film < ActiveRecord::Base
  has_many :ratings, :as => :rateable

  def rating_for(field)
    ratings.find_by_name(field).value
  end

  def rating_for=(field, value)
    rating = nil
    begin
      rating = ratigns.find_by_name(field)
      if rating.nil?
        rating = Rating.create!(:rateable => self, :name => field, :value => value)
      else
        rating.update_attributes!(:value => value)
      end
    rescue ActiveRecord::RecordInvalid
      self.errors.add_to_base(rating.errors.full_messages.join("\n"))
    end
  end

end

class Rating < ActiveRecord::Base
  # Has the following field:
  # column :rateable_id, Integer
  # column :name, String
  # column :value, Integer
  belongs_to :rateable, :polymorphic => true
  validates_inclusion_of :value, :in => (1..5)
  validates_uniqueness_of :name, :scope => :rateable_id

end

当然,通过这种方法,你可以在评级名称中进行复制,这并不是那么糟糕(标准化表格只是为了一个字段并不能解决问题)。

I don't think you need a plugin to do just that... you could do the following with AR

class Film < ActiveRecord::Base
  has_many :ratings, :as => :rateable

  def rating_for(field)
    ratings.find_by_name(field).value
  end

  def rating_for=(field, value)
    rating = nil
    begin
      rating = ratigns.find_by_name(field)
      if rating.nil?
        rating = Rating.create!(:rateable => self, :name => field, :value => value)
      else
        rating.update_attributes!(:value => value)
      end
    rescue ActiveRecord::RecordInvalid
      self.errors.add_to_base(rating.errors.full_messages.join("\n"))
    end
  end

end

class Rating < ActiveRecord::Base
  # Has the following field:
  # column :rateable_id, Integer
  # column :name, String
  # column :value, Integer
  belongs_to :rateable, :polymorphic => true
  validates_inclusion_of :value, :in => (1..5)
  validates_uniqueness_of :name, :scope => :rateable_id

end

Of course, with this approach you would have a replication in the Rating name, something that is not that bad (normalize tables just for one field doesn't cut it).

柠檬色的秋千 2024-08-26 12:26:09

您还可以使用插件 ajaxfull- rating

You can also use a plugin ajaxfull-rating

长伴 2024-08-26 12:26:09

这是另一个可能更强大的评级插件...它已经存在了一段时间,并且已经过修改以与 Rails 2 配合使用

http://agilewebdevelopment.com/plugins/acts_as_rateable

Here's another, possibly more robust rating plugin...it's been around for a while and has been revised to work with Rails 2

http://agilewebdevelopment.com/plugins/acts_as_rateable

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