an easy but hard-to-google question - how can i check whether an object has a field called "complete"? -- 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 -~----------~----~----~----~------~----~------~--~---
Is your context a Model: meaning it has the field at all, as in it exists in the database, or whether the field has been filled in a form? Or some other context? On Sep 6, 5:12 pm, Max Williams <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> an easy but hard-to-google question - how can i check whether an object > has a field called "complete"? > -- > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---
Max Williams
2007-Sep-06 16:27 UTC
Re: how do i check if an object has a particular field?
Mindtonic wrote:> Is your context a Model: meaning it has the field at all, as in it > exists in the database, or whether the field has been filled in a > form? > > Or some other context? > > On Sep 6, 5:12 pm, Max Williams <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>A model: i have a method that takes an object that can be either a story or an article. Story has a boolean field called ''complete'', article doesn''t. I want to check that the object has the field before i query the contents of the field. thanks max -- 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 -~----------~----~----~----~------~----~------~--~---
This is a good one. I thought I might be able to help. I tried lots of things in the console and searched through the api, but everything yields an error when the object does not have the requested property. I tried things like blank? and empty? but got the same results. I looked for something like object.properties to match object.methods thinking you could test against the result but that does not seem to exist either. That one would be the most handy, it does exist in the php reflections. Are you using this particular property to determine which type of model the method is receiving? If so, you could use either object.class == Article or object.is_a?(Article) ... something like that. Sorry I couldn''t be more help than that! On Sep 6, 5:27 pm, Max Williams <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Mindtonic wrote: > > Is your context a Model: meaning it has the field at all, as in it > > exists in the database, or whether the field has been filled in a > > form? > > > Or some other context? > > > On Sep 6, 5:12 pm, Max Williams <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > A model: i have a method that takes an object that can be either a > story or an article. Story has a boolean field called ''complete'', > article doesn''t. I want to check that the object has the field before i > query the contents of the field. > > thanks > max > -- > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---
Rob Biedenharn
2007-Sep-06 17:33 UTC
Re: how do i check if an object has a particular field?
On Sep 6, 2007, at 1:27 PM, Mindtonic wrote:> > On Sep 6, 5:27 pm, Max Williams <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: >> Mindtonic wrote: >>> Is your context a Model: meaning it has the field at all, as in it >>> exists in the database, or whether the field has been filled in a >>> form? >> >>> Or some other context? >> >>> On Sep 6, 5:12 pm, Max Williams <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> >> >> A model: i have a method that takes an object that can be either a >> story or an article. Story has a boolean field called ''complete'', >> article doesn''t. I want to check that the object has the field >> before i >> query the contents of the field. >> >> thanks >> max >> -- >> Posted viahttp://www.ruby-forum.com/. > > This is a good one. I thought I might be able to help. I tried lots > of things in the console and searched through the api, but everything > yields an error when the object does not have the requested property. > I tried things like blank? and empty? but got the same results. I > looked for something like object.properties to match object.methods > thinking you could test against the result but that does not seem to > exist either. That one would be the most handy, it does exist in the > php reflections. > > Are you using this particular property to determine which type of > model the method is receiving? If so, you could use either > object.class == Article or object.is_a?(Article) ... something like > that. Sorry I couldn''t be more help than that!Shouldn''t you be able to say: object.respond_to?(:complete) You can also duck-type it: class Article def complete nil # or true depending on your semantics end end Then you don''t need to check first. -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.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?hl=en -~----------~----~----~----~------~----~------~--~---
Howdy, Why can''t #instance_variables work here? It''s not what I would do but looks like it can be used to return an array of an object''s instance variables. Alternatives: - already mentioned #is_a? - base class for both objects with appropriate behavior - method_missing magic on Article to respond appropriately (could get sticky) Cheers, Mel On 9/6/07, Mindtonic <mindtonic-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > This is a good one. I thought I might be able to help. I tried lots > of things in the console and searched through the api, but everything > yields an error when the object does not have the requested property. > I tried things like blank? and empty? but got the same results. I > looked for something like object.properties to match object.methods > thinking you could test against the result but that does not seem to > exist either. That one would be the most handy, it does exist in the > php reflections. > > Are you using this particular property to determine which type of > model the method is receiving? If so, you could use either > object.class == Article or object.is_a?(Article) ... something like > that. Sorry I couldn''t be more help than that! > > On Sep 6, 5:27 pm, Max Williams <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: > > Mindtonic wrote: > > > Is your context a Model: meaning it has the field at all, as in it > > > exists in the database, or whether the field has been filled in a > > > form? > > > > > Or some other context? > > > > > On Sep 6, 5:12 pm, Max Williams <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > > > A model: i have a method that takes an object that can be either a > > story or an article. Story has a boolean field called ''complete'', > > article doesn''t. I want to check that the object has the field before i > > query the contents of the field. > > > > thanks > > max > > -- > > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---
George Bailey
2007-Sep-06 17:43 UTC
Re: how do i check if an object has a particular field?
On Sep 6, 2007, at 10:12 AM, Max Williams wrote:> an easy but hard-to-google question - how can i check whether an > object > has a field called "complete"?the_object.respond_to?("complete") Should work. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Max Williams
2007-Sep-06 20:13 UTC
Re: how do i check if an object has a particular field?
George Bailey wrote:> On Sep 6, 2007, at 10:12 AM, Max Williams wrote: > >> an easy but hard-to-google question - how can i check whether an >> object >> has a field called "complete"? > > > > the_object.respond_to?("complete") > > Should work.yep, that''s what i''m after! thanks everyone :) -- 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 -~----------~----~----~----~------~----~------~--~---
Matthew Rudy
2007-Sep-06 20:14 UTC
Re: how do i check if an object has a particular field?
Max Williams wrote:> an easy but hard-to-google question - how can i check whether an object > has a field called "complete"?Model.column_names>> User.column_names=> ["id", "username", "password", "name", "email", "showemail", "notifications", "age", "sex", "location_id", "interests", "music", "comments", "image_id", "status", "lastlogin", "lastip", "session_id", "loggedin"] -- 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 -~----------~----~----~----~------~----~------~--~---
Rob Biedenharn
2007-Sep-06 20:34 UTC
Re: how do i check if an object has a particular field?
On Sep 6, 2007, at 4:14 PM, Matthew Rudy wrote:> Max Williams wrote: >> an easy but hard-to-google question - how can i check whether an >> object >> has a field called "complete"? > > Model.column_names > >>> User.column_names > => ["id", "username", "password", "name", "email", "showemail", > "notifications", "age", "sex", "location_id", "interests", "music", > "comments", "image_id", "status", "lastlogin", "lastip", "session_id", > "loggedin"]Which answers the slightly different question: Does the database sitting under this Model contain a column called "complete"? The OP asked about an object (and in particular and ActiveRecord object), so respond_to? is probably a more apropos answer. ActiveRecord overrides respond_to? so that it is true for attributes which may actually be handled by method_missing when actually called. Consider if the OP had: class Article def complete read_attribute(:finished) end end In that case, Article.column_names.include?("complete") == false even though Article.new.respond_to?("complete") == true -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.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?hl=en -~----------~----~----~----~------~----~------~--~---
On Thu, 6 Sep 2007 16:34:02 -0400, Rob Biedenharn wrote:> The OP asked about an object (and in particular and ActiveRecord > object), so respond_to? is probably a more apropos answer. > ActiveRecord overrides respond_to? so that it is true for attributes > which may actually be handled by method_missing when actually called.Wasn''t there a recent change to core that may be important here? I remember reading something about cases where, in 1.2.3, respond_to? would be false because AR hadn''t looked at the table yet, while Edge would correctly respond true. Or something like that. Jay Levitt --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Matthew Rudy
2007-Sep-06 21:43 UTC
Re: how do i check if an object has a particular field?
Rob Biedenharn wrote:> On Sep 6, 2007, at 4:14 PM, Matthew Rudy wrote: >> "comments", "image_id", "status", "lastlogin", "lastip", "session_id", >> "loggedin"] > > Which answers the slightly different question: Does the database > sitting under this Model contain a column called "complete"? > > The OP asked about an object (and in particular and ActiveRecord > object), so respond_to? is probably a more apropos answer. > ActiveRecord overrides respond_to? so that it is true for attributes > which may actually be handled by method_missing when actually called.Max originally asked "how can i check whether an object has a field called ''complete''?" -- namely he is talking about the database field. but equally, I like the respond_to solutions, just thought, if we strictly want to look at "fields", that it was useful to point this out. -- better than calling "attributes.include?(field)" I feel -- 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 -~----------~----~----~----~------~----~------~--~---
Max Williams
2007-Sep-07 07:55 UTC
Re: how do i check if an object has a particular field?
Matthew Rudy wrote:> Rob Biedenharn wrote: >> On Sep 6, 2007, at 4:14 PM, Matthew Rudy wrote: >>> "comments", "image_id", "status", "lastlogin", "lastip", "session_id", >>> "loggedin"] >> >> Which answers the slightly different question: Does the database >> sitting under this Model contain a column called "complete"? >> >> The OP asked about an object (and in particular and ActiveRecord >> object), so respond_to? is probably a more apropos answer. >> ActiveRecord overrides respond_to? so that it is true for attributes >> which may actually be handled by method_missing when actually called. > > Max originally asked "how can i check whether an object has a field > called ''complete''?" -- namely he is talking about the database field. > > but equally, > I like the respond_to solutions, > just thought, if we strictly want to look at "fields", that it was > useful to point this out. > -- better than calling "attributes.include?(field)" I feelYeah, this is a useful distinction to know about, thanks Rob (and everyone else). In this particular case responds_to is fine, but it''s good to know about column_names.include? as well, since it''s actually a bit more ''accurate'' like you say. -- 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 -~----------~----~----~----~------~----~------~--~---