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.'
  end

  def exists? (topic_id)
    u = User.find_by_id(@user.id, :include => 'topics', :conditions => ["topics.id=?", topic_id])
    true if !u.nil?
  end


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!

Re: Optimized way of checking before database inserts

The first bit of validation you probably will want to keep, since it checks to make sure that the topic ID in the parameter is valid.

I'd probably write everything after line 2 as:

topic = Topic.find(id)

if !@user.topics.include?(topic)
  @user.topics << topic
  topic.title + " was successfully saved."
else
  return "Topic already saved!"
end

Re: Optimized way of checking before database inserts

Great, let me give that a shot!

Re: Optimized way of checking before database inserts

Another problem I found with the following:

return "Topic already saved!" if self.exists?(id)

is that it continues to execute the rest of the code,
instead of returning--yielding an activerecord exception
when it tries to reinsert the same primary key

i had to change it to:

    if !Topic.exists?(id)
      return "Unable to save topic " + id.to_s
    elsif exists?(id)
      return "Topic already saved!"
    else
      topic = Topic.find(id)
      topic.users << @user
      topic.title + ' was successfully saved.'
    end

to get it to work the way i wanted.

Re: Optimized way of checking before database inserts

And now I seem to have another issue,
when I remove a topic or two and then try saving
it says "Topic already saved"

  def save (id)
    if !Topic.exists?(id)
      "Unable to find that topic"
    else topic = Topic.find(id)
      if !@user.topics.include?(topic)
        @user.topics << topic
        topic.title + " was successfully saved."
      else
        "Topic already saved!"
      end
    end
  end
 
  def remove (id)
    if !Topic.exists?(id)
      "Unable to find that topic"
    else topic = Topic.find(id)
      topic.users.delete @user  #ignore remove if it's already gone
      topic.title + ' was successfully removed.'
    end
   end

And, it's not like the they're still in the database,
because I checked that too...

Re: Optimized way of checking before database inserts

Ok, so sometimes when you update the database, you have to restart your server, then get rid of the cache, then just reboot your computer and everything seems to work ok smile