Hi all,

I have this method which, ideally, I would like to work in such a way as
I pass in three string values, the first is the name of the object I am
trying to instantiate, the second a string value corresponding to an
attribute for that object and finally the value that the attribute gets
updated with.

The second half of this I have working, but instantiating an object from
a string value is not working.

I have done several searches and have come up with a solution that runs
the correct query but doesn't seem to create an actual object. Here is
what I have:

params => {:model => "user", :attr => "name", "user" => { "name" =>
"John"}}

def save_update
@obj = Object.const_get(:"#{params[:model]}").find(:first,
:conditions =>
["upper(#{params[:attr]}) = ?",
params[:"#{params[:model]}"][:"#{params[:attr]}"]])

if !@obj.blank?

end
end

When I get to the line checking to make sure the object is not blank
results in failure.

I guess my question is is this even possible and does anybody have any
suggestion on how to make this work? Thanks,

-S

--
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@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Shandy Nantz at Apr 23, 2012 at 8:14 pm
    Even when I try something like:

    @obj = "#{params[:model]}".constantize
    @this = @obj.find(:first, :conditions => ["upper(#{params[:attr]}) = ?",
    params[:"#{params[:model].downcase}"][:"#{params[:attr]}"]])

    I still have a null object.

    --
    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@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Colin Law at Apr 23, 2012 at 8:30 pm

    On 23 April 2012 21:13, Shandy Nantz wrote:
    Even when I try something like:

    @obj = "#{params[:model]}".constantize
    @this = @obj.find(:first, :conditions => ["upper(#{params[:attr]}) = ?",
    params[:"#{params[:model].downcase}"][:"#{params[:attr]}"]])

    I still have a null object.
    Why complicate things with the conditions when you are trying to get
    the basic find going? What if you just do @obj.find :first
    Look in development.log to see what query is being run.

    Colin

    --
    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@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Shandy Nantz at Apr 27, 2012 at 4:35 pm
    My original code actually did work, I was having issues with another bit
    of the code which was confusing the whole matter. Thanks for the
    responses. Hopefully this helps someone, thanks,

    -S

    --
    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@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • masta Blasta at Apr 27, 2012 at 8:55 pm
    I would go with

    @obj = "klass_name".camelize.constantize

    and then make use to use
    @obj.responds_to? :attribute

    to test for existence before you start finding and assigning

    --
    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@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedApr 23, '12 at 4:32p
activeApr 27, '12 at 8:55p
posts5
users2
websiterubyonrails.org
irc#RubyOnRails

2 users in discussion

masta Blasta: 4 posts Colin Law: 1 post

People

Translate

site design / logo © 2022 Grokbase