I have a model Item, which users can vote on by creating either a new UpVote or DownVote. When a UpVote or Downvote is created, it records the user's ip address and the Item's id. I want to list an array of the first 100 Items that have not been voted on by the current user's ip.
So far, here's what I have:
schema
create_table "up_votes", force: true do |t|
t.string "ip"
end
create_table "down_votes", force: true do |t|
t.string "ip"
end
model
class Item < ActiveRecord::Base
def up_votes_array
self.up_votes.map(&:ip).to_a
end
def down_votes_array
self.down_votes.map(&:ip).to_a
end
def up_voted?(ip)
self.up_votes_array.include? ip
end
def down_voted?(ip)
self.down_votes_array.include? ip
end
controller
@not_voted = Thing.where(show: true).select { |item| !item.up_voted?(request.remote_ip) }.select { |thing| !item.down_voted?(request.remote_ip) }.sort_by(&:alphabetical).reverse.first(100).shuffle
It works, but there's something about all that that seems unnecessarily complicated, and I'm worried that as my database grows, it may become inefficient. Is there a more efficient way to get this array?
I'm using Rails 4 and Sqlite3.
Aucun commentaire:
Enregistrer un commentaire