プラグイン名
acts_as_ratedプラグイン
このプラグインができること
- レーティング出来る
- 平均ポイント・ポイント総和・投票者数取得可能
- ユーザ削除によるポイント変動(不正ユーザによる操作のキャンセルが楽かも)
- ユーザIDからレーティング済みモデルを抽出可能
- 平均ポイントによるレーティング対象モデル抽出可能
詳しいことは本家参照↓
* Rate any model
* Optionally add fields to the rated objects to optimize speed
* Optionally add an external rating statistics table with a record for each rated model
* Can work with the added fields, external table or just using direct SQL count/avg calls
* Use any model as the rater (defaults to User)
* Limit the range of the ratings
* Average, total and number of ratings
* Find objects by ratings or rating ranges
* Find objects by rater
* Check if an object is rated by a specific rater. (Added by Tiago Serafim, thanks!)
* Extensively tested
対象バージョン
1.2系 2.0系 2.1系
ちょー簡単な使い方
./script/plugin install svn://rubyforge.org/var/svn/acts-as-rated/trunk/acts_as_rated
でインストールして、
class AddRatingTables < ActiveRecord::Migration
def self.up
ActiveRecord::Base.create_ratings_table
# Movies table has the columns for the ratings added while it's created
create_table(:movies) do |t|
t.column :title, :text
Movie.generate_ratings_columns t
end
# Cars table has the columns for the ratings added, but after the fact, using ALTER TABLE calls.
# Usually used if the model already exist and we want to add the ratings after the fact
create_table(:cars) do |t|
t.column :title, :text
end
Car.add_ratings_columns
end
def self.down
# Remove the columns we added
Car.remove_ratings_columns
drop_table :movies rescue nil
drop_table :cars rescue nil
ActiveRecord::Base.drop_ratings_table
end
end
みたいにマイグレーションしてからモデルに、
class Book < ActiveRecord::Base
acts_as_rated
end
としておくと、こんなことができる。
bill = User.find_by_name 'bill'
jill = User.find_by_name 'jill'
catch22 = Book.find_by_title 'Catch 22'
hobbit = Book.find_by_title 'Hobbit'
catch22.rate 5, bill
hobbit.rate 3, bill
catch22.rate 1, jill
hobbit.rate 5, jill
hobbit.rating_average # => 4
hobbit.rated_total # => 8
hobbit.rated_count # => 2
hobbit.unrate bill
hobbit.rating_average # => 5
hobbit.rated_total # => 5
hobbit.rated_count # => 1
bks = Book.find_by_rating 5 # => [hobbit]
bks = Book.find_by_rating 1..5 # => [catch22, hobbit]
usr = Book.find_rated_by jill # => [catch22, hobbit]
普通のマイグレーションファイル作ってから、レーティングしたいモデルだけAR.generate_ratings_columnsとかで追加マイグレーションするのが良いかも。
公式ページ
日本語解説ページ
外国語解説ページ
のうはう
- マイグレーション時(あらかじめyour_tablesとyour2_tablesはDB/Model共に必要な構造を作っておき、追加マイグレーションする)
class AddRatingTables < ActiveRecord::Migration
def self.up
ActiveRecord::Base.create_ratings_table
YourTable.add_ratings_columns
Your2Table.add_ratings_columns
end
def self.down
YourTable.remove_ratings_columns
Your2Table.remove_ratings_columns
ActiveRecord::Base.drop_ratings_table
end
end
コメント
最終更新:2008年08月20日 21:53