I have an ActiveRecord model called User, here''s my migration and
model:
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.column :email, :string, :null => false
t.column :full_name, :string, :null => false
end
end
def self.down
drop_table :users
end
end
______
class User < ActiveRecord::Base
end
A user has a status. A status is something (a string: "ready",
"waiting") that has to be evaluated and cannot be stored to the
database.
My question is about where I should implement the logic (get_status)
to get the user''s status: in the model or the controller? The
controller seems the most logical to me.
I would like to be able to get the status like this, e.g.:
user=User.find(1);
user.status
=> "ready"
So, to implement the above example I would have to have a method
get_status in my user controller? e.g.:
class UsersController < ApplicationController
def show
@user=User.find(1)
@user.get_status
render :json => @user.to_json(:include => @status ) # this should
include the status
end
def get_status
"Ready" # hard coding for now...
end
end
So after the call to show I would get a json string back representing
the User, right?
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
Definitely in the model, business logic should be in the model, the controller should transform the model for display. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
To achieve this:>> user=User.find(1); >> user.status >> => "ready"You should a method in the model. Then you can access this method anywhere you have a User object. If you put it in the controller, you will have to repeat yourself everytime you want User.status In your model: def status "read" if.... etc. end In your controller def show @user=User.find(1) #your show view can now read the status method end HTH --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Here''s my user model with the status logic:
class User < ActiveRecord::Base
attr :status, true
def eval_status # this needs to be called anytime User is set
@status="Ready"
end
end
So now how do I have eval_status called anytime I call User.find..?
On Aug 24, 4:43 am, "toby privett"
<tobypriv...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> To achieve this:
>
> >> user=User.find(1);
> >> user.status
> >> => "ready"
>
> You should a method in the model. Then you can access this method
> anywhere you have a User object. If you put it in the controller, you
> will have to repeat yourself everytime you want User.status
>
> In your model:
>
> def status
> "read" if....
> etc.
> end
>
> In your controller
>
> def show
> @user=User.find(1)
> #your show view can now read the status method
> end
>
> HTH
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
With status now implimented in the model, when I call: u=User.find(1) u.status => nil status isn''t set so it''s null. I need eval_status to be called somehow. Any idea? On Aug 24, 4:43 am, "toby privett" <tobypriv...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> To achieve this: > > >> user=User.find(1); > >> user.status > >> => "ready" > > You should a method in the model. Then you can access this method > anywhere you have a User object. If you put it in the controller, you > will have to repeat yourself everytime you want User.status > > In your model: > > def status > "read" if.... > etc. > end > > In your controller > > def show > @user=User.find(1) > #your show view can now read the status method > end > > HTH--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Why don''t you just have the status method return what you need?
class User
def status
"Ready"
end
end
Pat
On 8/24/07, eggie5 <eggie5-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:>
> With status now implimented in the model, when I call:
>
> u=User.find(1)
>
> u.status
>
> => nil
>
> status isn''t set so it''s null. I need eval_status to be
called
> somehow.
>
> Any idea?
>
> On Aug 24, 4:43 am, "toby privett"
<tobypriv...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> > To achieve this:
> >
> > >> user=User.find(1);
> > >> user.status
> > >> => "ready"
> >
> > You should a method in the model. Then you can access this method
> > anywhere you have a User object. If you put it in the controller, you
> > will have to repeat yourself everytime you want User.status
> >
> > In your model:
> >
> > def status
> > "read" if....
> > etc.
> > end
> >
> > In your controller
> >
> > def show
> > @user=User.find(1)
> > #your show view can now read the status method
> > end
> >
> > HTH
>
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
This does the trick:
def evaluate_status
''Waiting''
end
def status
@status ||= evaluate_status
end
> class User
> def status
> "Ready"
> end
> end
>
> Pat
>
> On 8/24/07, eggie5 <egg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:
>
>
>
> > With status now implimented in the model, when I call:
>
> > u=User.find(1)
>
> > u.status
>
> > => nil
>
> > status isn''t set so it''s null. I need eval_status to
be called
> > somehow.
>
> > Any idea?
>
> > On Aug 24, 4:43 am, "toby privett"
<tobypriv...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> > > To achieve this:
>
> > > >> user=User.find(1);
> > > >> user.status
> > > >> => "ready"
>
> > > You should a method in the model. Then you can access this method
> > > anywhere you have a User object. If you put it in the controller,
you
> > > will have to repeat yourself everytime you want User.status
>
> > > In your model:
>
> > > def status
> > > "read" if....
> > > etc.
> > > end
>
> > > In your controller
>
> > > def show
> > > @user=User.find(1)
> > > #your show view can now read the status method
> > > end
>
> > > HTH
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
On 8/24/07, eggie5 <eggie5-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > With status now implimented in the model, when I call: > > u=User.find(1) > > u.status > > => nil > > status isn''t set so it''s null. I need eval_status to be called > somehow. > > Any idea?put your initialization code into an after_initialize method, then it''ll be called both when returning a user via User.new or User.find. Also, I know you don''t want to store your status or state in the database, but I just thought I''d mention the acts_as_state_machine plugin since it may be of some use to you (http://rails.aizatto.com/category/plugins/acts_as_state_machine/) Adam --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
No... do not use after_initialize if you can avoid it. There are huge performance penalties, as in... it will be called any time you do a .new, or each time you create an instance. A finder that gets 50 records will call this code 50 times, and you can''t turn it off. If you look at the rails source, you''ll see comments basically warning you not to use after_initialize and after_find. The best approach is to override the accessor as eggie5 suggested: def evaluate_status ''Waiting'' end def status @status ||= evaluate_status end On 8/24/07, Adam Cohen <bionicboogie-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > On 8/24/07, eggie5 <eggie5-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > With status now implimented in the model, when I call: > > > > u=User.find(1) > > > > u.status > > > > => nil > > > > status isn''t set so it''s null. I need eval_status to be called > > somehow. > > > > Any idea? > > put your initialization code into an after_initialize method, then > it''ll be called both when returning a user via User.new or User.find. > > Also, I know you don''t want to store your status or state in the > database, but I just thought I''d mention the acts_as_state_machine > plugin since it may be of some use to you > (http://rails.aizatto.com/category/plugins/acts_as_state_machine/) > > Adam > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thanks for you help everyone. I remember seeing something like this in
my rails book. It works great now just using the accessor like this:
def status
@status ||= eval_status # eval_status does the work to return
the status
end
I just didn''t know that ruby would automatically call it. So it just
boils down to my ignorance of ruby.
On Aug 24, 12:28 pm, "Brian Hogan"
<bpho...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> No... do not use after_initialize if you can avoid it. There are huge
> performance penalties, as in... it will be called any time you do a .new,
or
> each time you create an instance. A finder that gets 50 records will call
> this code 50 times, and you can''t turn it off. If you look at the
rails
> source, you''ll see comments basically warning you not to use
> after_initialize and after_find.
>
> The best approach is to override the accessor as eggie5 suggested:
>
> def evaluate_status
> ''Waiting''
> end
>
> def status
> @status ||= evaluate_status
> end
>
> On 8/24/07, Adam Cohen
<bionicboo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>
>
>
> > On 8/24/07, eggie5
<egg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>
> > > With status now implimented in the model, when I call:
>
> > > u=User.find(1)
>
> > > u.status
>
> > > => nil
>
> > > status isn''t set so it''s null. I need
eval_status to be called
> > > somehow.
>
> > > Any idea?
>
> > put your initialization code into an after_initialize method, then
> > it''ll be called both when returning a user via User.new or
User.find.
>
> > Also, I know you don''t want to store your status or state in
the
> > database, but I just thought I''d mention the
acts_as_state_machine
> > plugin since it may be of some use to you
> > (http://rails.aizatto.com/category/plugins/acts_as_state_machine/)
>
> > Adam
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
On 8/24/07, Brian Hogan <bphogan-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> No... do not use after_initialize if you can avoid it. There are huge > performance penalties, as in... it will be called any time you do a .new, or > each time you create an instance. A finder that gets 50 records will call > this code 50 times, and you can''t turn it off. If you look at the rails > source, you''ll see comments basically warning you not to use > after_initialize and after_find.thanks for the heads up Brian, I wasn''t aware of the performance penalty that after_initialize might incur. Adam --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---