Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
How do you use Observers?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  6 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Daniel Waite  
View profile  
 More options Oct 19 2007, 5:48 pm
From: Daniel Waite <rails-mailing-l...@andreas-s.net>
Date: Fri, 19 Oct 2007 23:48:03 +0200
Local: Fri, Oct 19 2007 5:48 pm
Subject: How do you use Observers?
Clarification: I'm asking how YOU use them, not how TO use them.

I ask because after reading a post over at Pivotal Blabs (a very good
blog, btw), it appears the Observer isn't quite meant to do what I
thought it was. Example:

class UserObserver

  def before_validation(user)
    user.errors.add_to_base("Can't delete.") if user.admin?
  end

end

I would expect the above to halt the saving of the object because it now
fails validation. But it doesn't. The record gets saved anyway.

Then I tried...

class UserObserver

  def before_validation(user)
    user.admin?
  end

end

Perhaps returning false would halt the process. Not so. In fact, the
only way to stop a save inside an observer is to raise an exception:

class UserObserver

  def before_validation(user)
    raise StandardError if user.admin?
  end

end

But I don't want to raise an exception. It's not an exceptional
situation -- the model simply failed validation.

Meanwhile, I've recently discovered you can do this...

class User

  validate :there_can_be_only_one_admin
  validate :another_validation
  validate :yet_another

end

I've seen the validate method before, but I didn't know you could stack
it like that. Anywho, if you add errors to the object inside any of
these methods, validation fails, which is fine.

But I'm left with wondering... what's the purpose of an Observer outside
of sending e-mails and logging messages?

So, how do you use Observers in your projects?
--
Posted via http://www.ruby-forum.com/.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Daniel Waite  
View profile  
 More options Oct 19 2007, 5:51 pm
From: Daniel Waite <rails-mailing-l...@andreas-s.net>
Date: Fri, 19 Oct 2007 23:51:27 +0200
Local: Fri, Oct 19 2007 5:51 pm
Subject: Re: How do you use Observers?

Daniel Waite wrote:
> class UserObserver

>   def before_validation(user)
>     user.admin?
>   end

> end

Sorry, that should read:

class UserObserver

  def before_validation(user)
    return false if user.admin?
    true
  end

end
--
Posted via http://www.ruby-forum.com/.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
wentwj@gmail.com  
View profile  
 More options Oct 20 2007, 1:02 am
From: "wen...@gmail.com" <wen...@gmail.com>
Date: Sat, 20 Oct 2007 05:02:49 -0000
Local: Sat, Oct 20 2007 1:02 am
Subject: Re: How do you use Observers?
I'm relatively new to rail, and have only done minimal work with
observers, but I've seen them used for caching purposes.  To remove
cached versions of pages after a model has been updated/saved, and
things of that nature.  I've never considered using them for model
validation, as you point out that work belongs in the validation area
of the model, not in an observer.

On Oct 19, 4:51 pm, Daniel Waite <rails-mailing-l...@andreas-s.net>
wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Daniel Waite  
View profile  
 More options Oct 20 2007, 1:23 am
From: Daniel Waite <rails-mailing-l...@andreas-s.net>
Date: Sat, 20 Oct 2007 07:23:17 +0200
Local: Sat, Oct 20 2007 1:23 am
Subject: Re: How do you use Observers?

wen...@gmail.com wrote:
> I'm relatively new to rail, and have only done minimal work with
> observers, but I've seen them used for caching purposes.  To remove
> cached versions of pages after a model has been updated/saved, and
> things of that nature.

Interesting. I've never dealt with page caching, but that sounds like a
good candidate for observers.

> I've never considered using them for model validation, as
> you point out that work belongs in the validation area of
> the model, not in an observer.

And I guess I'm realizing that the observer isn't the place for
validation. Looking back it seems silly. The observer's role is to react
based on what it observes, not interfere with the models actions.

Thanks for the reply!
--
Posted via http://www.ruby-forum.com/.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Wincent Colaiuta  
View profile  
 More options Oct 20 2007, 7:51 am
From: Wincent Colaiuta <w...@wincent.com>
Date: Sat, 20 Oct 2007 04:51:29 -0700
Local: Sat, Oct 20 2007 7:51 am
Subject: Re: How do you use Observers?

I ran into exactly the same problem when I first began experimenting
with observers:

http://dev.rubyonrails.org/ticket/7968


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Daniel Waite  
View profile  
 More options Oct 21 2007, 12:06 am
From: Daniel Waite <rails-mailing-l...@andreas-s.net>
Date: Sun, 21 Oct 2007 06:06:18 +0200
Local: Sun, Oct 21 2007 12:06 am
Subject: Re: How do you use Observers?

Wincent Colaiuta wrote:
> I ran into exactly the same problem when I first began experimenting
> with observers:

> http://dev.rubyonrails.org/ticket/7968

Interesting activity. I wonder what will come of it. It bothered me more
when I expected the observer to be able to halt the process of a save.
Now that I no longer have that expectation it doesn't bother me so much.

I dunno what the exact role of an observer should be, so I can't really
comment on it. Unless, of course, I defined my own role... But that's up
to the core team. Though I'm sure a cunning Rubyist could achieve any
desired effect via a plugin.

Perhaps, perhaps...
--
Posted via http://www.ruby-forum.com/.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google