Topic: Optimized way of checking before database inserts
So far, I'm doing FOUR database actions for a save... there has to be a better way.
Could someone show me?
def save (id)
return "Unable to save topic " + id.to_s if !Topic.exists?(id)
return "Topic already saved!" if self.exists?(id)
topic = Topic.find(id)
topic.users << @user
topic.title + ' was successfully saved.'
def exists? (topic_id)
u = User.find_by_id(@user.id, :include => 'topics', :conditions => ["topics.id=?", topic_id])
true if !u.nil?
users - topics_users - topics
1) I have the Topic.exists? check through active record that the topic exists
2) my own exists? checks to see that the join table primary key doesn't already exists
3) then I find the Topic
4) then I save it...
the only thing I'm not checking is the user to make sure they're still there!