If I do a find such as
order = Order.find_by_name("John")
will ''order'' be set to false ?
I want to make a condition where if no record is found , something happens.
So far no luck with the above though , thought I could confirm
TIA
Stuart
--
http://en.wikipedia.org/wiki/Dark_ambient
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
Hi Stuart, Dark Ambient wrote:> If I do a find such as > order = Order.find_by_name("John") > will ''order'' be set to false ?I believe you''ll find that what ''find'' returns depends on what you ask it for. I think if you ask it to ''find first'' it returns either a single record or nil, and if you ask it to ''find all'' it returns a (possibly empty) array. hth, Bill --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Find will return nil when no record is found - so order will effectively be
false.
This should work for you:
order = Order.find_by_name("John")
unless order
# handle record not found
end
On 9/19/06, Dark Ambient
<sambient-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:>
>
> If I do a find such as
> order = Order.find_by_name("John")
> will ''order'' be set to false ?
> I want to make a condition where if no record is found , something
> happens.
> So far no luck with the above though , thought I could confirm
>
> TIA
> Stuart
>
>
> --
> http://en.wikipedia.org/wiki/Dark_ambient
>
> >
>
--
Matt Jones
mdj.acme-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
President/Technical Director, Acme Art Company (acmeartco.org)
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
Here''s my problem now :) I am attempting to check for records before
allowing user to do a create. The problem is in the first line.
current_user.id should be available because I can get it to print out
in various views and it''s in the application.rb controller. However
it doesn''t seem to take. To test my method , if i say user_id = 10,
then things work.
in the Cdetail model user_id is a method (column). So basically what
I''m trying to say here is count how many records user_id with the
value of current_user.id has.
i.e. right now i''m the only one on the system, so my current_user.id
is 10. Somehow this translation is not taking place ?
def check_forec
@result = Cdetail.count "user_id = current_user.id"
if @result >= 1
flash.now[:warning] = ''Record Alread exists''
redirect_to(:controller => ''index'', :action =>
''index'')
end
end
Stuart
On 9/19/06, Matt Jones <mdj.acme-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> Find will return nil when no record is found - so order will effectively be
> false.
>
> This should work for you:
>
>
> order = Order.find_by_name("John")
> unless order
> # handle record not found
> end
>
>
>
> On 9/19/06, Dark Ambient
<sambient-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> >
> > If I do a find such as
> > order = Order.find_by_name("John")
> > will ''order'' be set to false ?
> > I want to make a condition where if no record is found , something
> happens.
> > So far no luck with the above though , thought I could confirm
> >
> > TIA
> > Stuart
> >
> >
> > --
> > http://en.wikipedia.org/wiki/Dark_ambient
> >
> >
> >
> >
>
>
>
> --
> Matt Jones
> mdj.acme-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
> President/Technical Director, Acme Art Company (acmeartco.org )
>
> >
>
--
http://en.wikipedia.org/wiki/Dark_ambient
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
On 9/19/06, Dark Ambient <sambient-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > Here''s my problem now :) I am attempting to check for records before > allowing user to do a create. The problem is in the first line. > current_user.id should be available because I can get it to print out > in various views and it''s in the application.rb controller. However > it doesn''t seem to take. To test my method , if i say user_id = 10, > then things work. > in the Cdetail model user_id is a method (column). So basically what > I''m trying to say here is count how many records user_id with the > value of current_user.id has. > i.e. right now i''m the only one on the system, so my current_user.id > is 10. Somehow this translation is not taking place ? > > > def check_forec > @result = Cdetail.count "user_id = current_user.id" > if @result >= 1 > flash.now[:warning] = ''Record Alread exists'' > redirect_to(:controller => ''index'', :action => ''index'') > end > end > > Stuart > >The string "user_id = current_user.id" is being passed straight into SQL, where it probably just gives an error. You want "user_id = #{current_user.id}", which will insert the value of current_user.id into the string. *However*, what you are doing seems like a major re-invention of the wheel - you''re doing all the work for a has_one association. You probably want your models to look like this: class Cdetail < ActiveRecord::Base belongs_to :user end class User < ActiveRecord::Base has_one :cdetail, :dependent => :destroy end The ":dependent => :destroy" part will ensure that Cdetail records are not left hanging around without associated User objects. With that set up, you can access the associated Cdetail record like this: current_user.cdetail and figure out if one exists with current_user.cdetail.nil? Hope this helps, -- Matt Jones mdj.acme-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org President/Technical Director, Acme Art Company (acmeartco.org) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 9/19/06, Matt Jones <mdj.acme-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > On 9/19/06, Dark Ambient <sambient-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Here''s my problem now :) I am attempting to check for records before > > allowing user to do a create. The problem is in the first line. > > current_user.id should be available because I can get it to print out > > in various views and it''s in the application.rb controller. However > > it doesn''t seem to take. To test my method , if i say user_id = 10, > > then things work. > > in the Cdetail model user_id is a method (column). So basically what > > I''m trying to say here is count how many records user_id with the > > value of current_user.id has. > > i.e. right now i''m the only one on the system, so my current_user.id > > is 10. Somehow this translation is not taking place ? > > > > > > def check_forec > > @result = Cdetail.count "user_id = current_user.id" > > if @result >= 1 > > flash.now[:warning] = ''Record Alread exists'' > > redirect_to(:controller => ''index'', :action => ''index'') > > end > > end > > > > Stuart > > > > > > The string "user_id = current_user.id" is being passed straight into SQL, > where > it probably just gives an error. You want "user_id = #{current_user.id}", > which > will insert the value of current_user.id into the string. > > *However*, what you are doing seems like a major re-invention of the wheel - > you''re doing all the work for a has_one association. You probably want your > models to look like this: > > class Cdetail < ActiveRecord::Base > belongs_to :user > end > > class User < ActiveRecord::Base > has_one :cdetail, :dependent => :destroy > end > > The ":dependent => :destroy" part will ensure that Cdetail records are not > left hanging around without associated User objects. > > With that set up, you can access the associated Cdetail record like this: > > current_user.cdetail > > and figure out if one exists with > > current_user.cdetail.nil? > > Hope this helps, > > -- > > Matt JonesMatt - Thank you so much and btw, apologies for the cross post. I think I could use some remedial Ruby and go back to the books. I completely forgot the #{ }. I did have the associations set up but no the destory but it makes sense. Stuart --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Take a look at the "validates_unique" validator, in addition to what Matt Jones has already said. On 9/19/06, Dark Ambient <sambient-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Here''s my problem now :) I am attempting to check for records before > allowing user to do a create. The problem is in the first line. > current_user.id should be available because I can get it to print out > in various views and it''s in the application.rb controller. However > it doesn''t seem to take. To test my method , if i say user_id = 10, > then things work. > in the Cdetail model user_id is a method (column). So basically what > I''m trying to say here is count how many records user_id with the > value of current_user.id has. > i.e. right now i''m the only one on the system, so my current_user.id > is 10. Somehow this translation is not taking place ? > > > def check_forec > @result = Cdetail.count "user_id = current_user.id" > if @result >= 1 > flash.now[:warning] = ''Record Alread exists'' > redirect_to(:controller => ''index'', :action => ''index'') > end > end > > Stuart > > On 9/19/06, Matt Jones <mdj.acme-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Find will return nil when no record is found - so order will effectively be > > false. > > > > This should work for you: > > > > > > order = Order.find_by_name("John") > > unless order > > # handle record not found > > end > > > > > > > > On 9/19/06, Dark Ambient <sambient-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > If I do a find such as > > > order = Order.find_by_name("John") > > > will ''order'' be set to false ? > > > I want to make a condition where if no record is found , something > > happens. > > > So far no luck with the above though , thought I could confirm > > > > > > TIA > > > Stuart > > > > > > > > > -- > > > http://en.wikipedia.org/wiki/Dark_ambient > > > > > > > > > > > > > > > > > > > > -- > > Matt Jones > > mdj.acme-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > > President/Technical Director, Acme Art Company (acmeartco.org ) > > > > > > > > > > -- > http://en.wikipedia.org/wiki/Dark_ambient > > > >-- When a man says he approves of something in principle, it means he hasn''t the slightest intention of putting it into practice. -- Bismarck --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 9/19/06, Matt Jones <mdj.acme-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> *However*, what you are doing seems like a major re-invention of the wheel - > you''re doing all the work for a has_one association. You probably want your > models to look like this: > > class Cdetail < ActiveRecord::Base > belongs_to :user > end > > class User < ActiveRecord::Base > has_one :cdetail, :dependent => :destroy > end > > The ":dependent => :destroy" part will ensure that Cdetail records are not > left hanging around without associated User objects. > > With that set up, you can access the associated Cdetail record like this: > > current_user.cdetail > > and figure out if one exists with > > current_user.cdetail.nil? > > Hope this helps, > > Matt JonesI''m having a hard time using ''current_user.cdetail.nil?'' in a before filter. Maybe it''s the way I''m writing it out. However the way things are set up cdetails/new will create a new record. I wanted the filter in the event that one existed. I guess it could be changed to ask if it''s ''not nil''. Stuart --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 9/19/06, Michael Campbell <michael.campbell-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Take a look at the "validates_unique" validator, in addition to what > Matt Jones has already said. >I thought about the validates_unique, however this is a situation where the user would create one record and then have the option later of edit and destroy. I wouldn''t want the user to enter in the information again only to have the form bounce back. Stuart --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 9/19/06, Dark Ambient <sambient-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > On 9/19/06, Michael Campbell <michael.campbell-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Take a look at the "validates_unique" validator, in addition to what > > Matt Jones has already said. > > > > > I thought about the validates_unique, however this is a situation > where the user would create one record and then have the option later > of edit and destroy. I wouldn''t want the user to enter in the > information again only to have the form bounce back.That''s a pretty common pattern - you can bounce it back with an error message, WITH all his information intact to retry or do whatever. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---