I think this is the classic scenario to use find_or_create_by_XXXX
# No 'Summer' tag exists
Tag.find_or_create_by_name("Summer") # equal to Tag.create(:name =>
"Summer")
# Now the 'Summer' tag does exist
Tag.find_or_create_by_name("Summer") # equal to Tag.find_by_name
("Summer")
you can see the documentation here
http://api.rubyonrails.org/classes/ActiveRecord/Base.html
On Jul 10, 9:53 pm, melomane <[email protected]> wrote:
> Hi
> In my web application, a table must have only one record. So when
> there is no record, user can add a new row to the table, (a add link
> is shown) and when a record is already available, user can't add a new
> one, he can just edit or destroy it( after destroying the current one,
> no more records exist, so he can add a new one). But I don't know how
> to check a table for emptiness.
> The other problem is when there is one record in table, how can I have
> a link to its show action(made by scaffolding), while i don't know the
> id. Because if the user destroy the record, its id is not 1 any more.
--~--~---------~--~----~------------~-------~--~----~
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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---