Peter Ehrlich wrote:
> Hi
> 
> I have website which displays tweets (from twitter).  Each tweet can
> either be given thumbsup or thumbsdown, but only once.  What would be
> the best way to organzine a database table for this?  Its a many-to-many
> relationship between tweets and users - should I make a database, lets
> call it 'pairs', so that each user will have_many :tweets through pairs?

You will need a join table -- it's impossible to do many-to-many 
relationships without it.  You can call it "pairs", though I might use 
"ratings".  Then:

class User
  has_many :pairs
  has_many :tweets, :through => :pairs
end

class Tweet
  has_many :pairs
  has_many :users, :through => :pairs
end

class Pair
  belongs_to :tweet
  belongs_to :user
end

Notes:
* You may not need the has_many :through associations at all.
* You might want to give the associations different names than the 
tables.
* You will definitely want foreign key constraints in the DB.  Use the 
foreign_key_migrations plugin.

> I should have validation coming from two directions - when displaying
> the tweet, it should check if the logged in user has already given the
> tweet thumbsup/thumbsdown,

current_user.tweet_ids.include? @tweet.id

> and every time an opinion is given, it should
> check if the tweet has already been reviewed by the logged in user.

This is best done with DB constraints.

> 
> I could see this database of pairs becoming very large, but fortunately
> there's really no reason to let users rate tweets after a certain date -
> so I could make a periodic job that removes old entries from the table
> after a week or so.

No, because then you'd lose your rating data.

> 
> I'm sure that I could work out a solution on my own, but this is
> somewhere I'd much rather be sure that I'm doing the right way, rather
> than having to go back an change it later for performance or
> maintainability reasons.

Understood.

>  Thanks in advance for any input!
> 
> Cheers,
> --Peter
> 
> PS - It would be nice to allow users without accounts to rate tweets.  I
> could store pair data in their session, but this would obviously not be
> bulletproof.  One big concern is if robots take advantage, rating 1000s
> of times by clearing their sessions and such.  Will be googling on this
> :-)

Then you'll need a way to identify anonymous users and make User records 
for them (or use a polymorphic association).

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
mar...@marnen.org
-- 
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to