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.
Hello, For the first part of your problem, you can check emptiness for your table Example with Example.find(:all).empty? => boolean For the second part, @object = Example.find(:first) your_path = example_path(@object) Of course you can probably do it with a better looking method, but basically that''s it. Cyril 2009/7/10 melomane <melomane21-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>> > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Nice approach, thanks I did it and it works now.
Another approach to checking for emptyness in case you need it: Table.count.zero? On Jul 10, 11:53 am, melomane <meloman...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 can also assign a variable and check for an empty array: find_data = model.find(:all) if find_data == [] # empty array # table is empty # do something end -- Posted via http://www.ruby-forum.com/.
Älphä Blüë wrote:> You can also assign a variable and check for an empty array: > > find_data = model.find(:all) > > if find_data == [] # empty array > # table is empty > # do something > endBut the variable is unnecessary, and in fact, a more concise way of saying the same thing would be if Model.find(:all).empty? Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
> But the variable is unnecessary, and in fact, a more concise way of > saying the same thing would be > > if Model.find(:all).empty?Agreed. I was just showing that there are more ways of doing things. I use your approach... .empty? .nil? .. two things I use heavily in my projects... -- Posted via http://www.ruby-forum.com/.
On Jul 12, 10:19 am, "Älphä Blüë" <rails-mailing-l...@andreas-s.net> wrote:> > But the variable is unnecessary, and in fact, a more concise way of > > saying the same thing would be > > > if Model.find(:all).empty? > > Agreed. I was just showing that there are more ways of doing things. I > use your approach... > > .empty? > .nil? > > .. two things I use heavily in my projects...Typically I use something like ''unless Model.first'', but are any of these techniques particularly lighter/faster on the db than others? -eric
On Jul 12, 7:15 pm, Eric <ericgh...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Jul 12, 10:19 am, "Älphä Blüë" <rails-mailing-l...@andreas-s.net> > wrote: > > > > But the variable is unnecessary, and in fact, a more concise way of > > > saying the same thing would be > > > > if Model.find(:all).empty? > > > Agreed. I was just showing that there are more ways of doing things. I > > use your approach... > > > .empty? > > .nil? > > > .. two things I use heavily in my projects... > > Typically I use something like ''unless Model.first'', but are any of > these techniques particularly lighter/faster on the db than others? >It won''t make much difference if you expect your table to genuinely only contain either 0 rows or a very small number of rows. If it might contain 10s or hundreds or rows it will be way faster to check Model.first than it would be to check Model.all.empty? (since you''d be loading instantiating all hundred objects rather than just 1) Fred> -eric
On Sun, Jul 12, 2009 at 11:28 AM, Frederick Cheung < frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > > On Jul 12, 7:15 pm, Eric <ericgh...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > On Jul 12, 10:19 am, "Älphä Blüë" <rails-mailing-l...@andreas-s.net> > > wrote: > > > > > > But the variable is unnecessary, and in fact, a more concise way of > > > > saying the same thing would be > > > > > > if Model.find(:all).empty? > > > > > Agreed. I was just showing that there are more ways of doing things. > I > > > use your approach... > > > > > .empty? > > > .nil? > > > > > .. two things I use heavily in my projects... > > > > Typically I use something like ''unless Model.first'', but are any of > > these techniques particularly lighter/faster on the db than others? > > > > It won''t make much difference if you expect your table to genuinely > only contain either 0 rows or a very small number of rows. If it might > contain 10s or hundreds or rows it will be way faster to check > Model.first than it would be to check Model.all.empty? (since you''d be > loading instantiating all hundred objects rather than just 1) > > FredI agree with Fred''s approach because if you''re testing for emptiness, you should do the least amount of work to determine it. For example, Model.first => will create 1 or no objects Model.all => will create table size of N or no objects Good luck, -Conrad> > > -eric > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Conrad Taylor wrote: [...]> I agree with Fred''s approach because if you''re testing for emptiness, > you > should do the least amount of work to determine it.Excellent point.> For example, > > Model.first => will create 1 or no objects > Model.all => will create table size of N or no objects >Better still: Model.count => will create one Numeric and that''s all!> Good luck, > > -ConradBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
On Jul 13, 2:41 am, Marnen Laibow-Koser <rails-mailing-l...@andreas- s.net> wrote:> Conrad Taylor wrote: > > [...] > > > I agree with Fred''s approach because if you''re testing for emptiness, > > you > > should do the least amount of work to determine it. > > Excellent point. > > > For example, > > > Model.first => will create 1 or no objects > > Model.all => will create table size of N or no objects > > Better still: > > Model.count => will create one Numeric and that''s all!In ruby land yes, this is cheaper but not for the database: with Model.first the database gets just one row and then stops, with Model.count it has to count the whole table (which can be expensive on a large table - usually the db will scan an appropriate index) Fred> > > Good luck, > > > -Conrad > > Best, > -- > Marnen Laibow-Koserhttp://www.marnen.org > mar...-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > -- > Posted viahttp://www.ruby-forum.com/.
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 <meloman...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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.