Philip Hallstrom wrote in post #968761:
...why are you trying to save a @topic that
you either a) already found or b) just created (and saved) ??
Sorry I got confused for a second and thought the create action by
default uses Topic.create() by default (when it actually uses
Topic.new()).
If you are concerned the topic might not get created right then...
def create
name = params[:topic][:name].titleize
@topic = Topic.find_or_create_by_name(name)
if @topic
current_user.topics << @topic
redirect_to @topic, :notice => 'Topic was successfully created.'
else
render :action => "new"
end
end
Thanks I tried that and it seems to be working, though it highlights a
problem of when a user tries to submit the same topic. So here's a
bashat my latest code (but it still gives me an error when the same user
tries to create the same topic 'Validation failed: User has already been
taken')
def create
name = params[:topic][:name].titleize.gsub(/\s+/, '')
@topic = Topic.find_or_create_by_name(name)
if @topic
if current_user.topics << @topic
redirect_to @topic, :notice => 'Topic was successfully
created.'
else
redirect_to @topic, :notice => 'You have already added this
topic'
end
else
render :action => "new"
end
end
(see my next bit below to see what I'm trying to achieve)
Actually I have just noticed, I have a validation for the
TopicAssociation model: validates_uniqueness_of :user_id, :scope =>
:topic_id
so I guess that's why it is failing - but how can I handle that error
more elegantly?
Ok. Then you'll need to set that up as well in the above code after
you've found the topic.
Hey... not sure why I thought of this before, but why is your create()
method *finding* a topic at all? You might want to see if there's a
better way to organize your actions...
What I'm trying to do in all the code above is based on..
Having a page which will have a form with the fields 'topic' and
'comment' (I'll come to dealing with comments later, just trying to get
topic working right now) and neither can be blank - Users are basically
adding 'topics' that interest them (which will show on their profile),
along with a comment about each topic. (And the homepage of the site
will simply list the most popular topics.)
So when they click submit, I want to first check that the topic doesn't
already exist (to prevent duplicate entries in the db - thus making it
easier to find which topics are the most popular) and if it does, to
simply add the association that they have added it (by
'current_user.topics << @topic' ?) (and then it will add the comment to
that topic too, but will deal with that later). If the topic doesn't
exist, it should get created, the association between user and topic be
made, and their comment get added (again, I'll deal with comments
later).
Does that make more sense about what I'm trying to do? (And why it's all
happening from one form). I don't want to add a 'see if this topic
exists first' page, as I would rather handle all that silently. Maybe
later I could add a 'live search' for the topic name field - but that's
a future feature lol
Ah yes. Missed that...
name.titleize.gsub(/\s+/, '')
Thanks! That's what I wanted - and I'll have to read up on what all of
that does too
(btw, as therean easy enough to strip anything that
isn't a-z or 1-0 from the string? That would help ensure records are
unique regardless of punctuation marks etc)