an easy but hard-to-google question - how can i check whether an object
has a field called “complete”?
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 W. [email protected]
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 W. [email protected]
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
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 W. [email protected]
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 Sep 6, 2007, at 10:12 AM, Max W. 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.
On Sep 6, 2007, at 1:27 PM, Mindtonic wrote:
On Sep 6, 5:12 pm, Max W. [email protected]
Posted viahttp://www.ruby-forum.com/.
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
George B. wrote:
On Sep 6, 2007, at 10:12 AM, Max W. 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
Max W. 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”]
On Sep 6, 2007, at 4:14 PM, Matthew R. 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.
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 B. wrote:
On Sep 6, 2007, at 4:14 PM, Matthew R. 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
On Thu, 6 Sep 2007 16:34:02 -0400, Rob B. 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 L.
Matthew R. wrote:
Rob B. wrote:
On Sep 6, 2007, at 4:14 PM, Matthew R. 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
Yeah, 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.