I''m working on a rake task that loads AR models from a yaml file, which fails when I try to use a deserialized object, as its class hasn''t been loaded. I don''t know beforehand exactly which classes are in the file(s), so need a way to load them dynamically, or load all the model classes. Thanks in advance, Isak --~--~---------~--~----~------------~-------~--~----~ 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 Jun 7, 8:11 am, "Isak Hansen" <isak.han...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m working on a rake task that loads AR models from a yaml file, > which fails when I try to use a deserialized object, as its class > hasn''t been loaded. > > I don''t know beforehand exactly which classes are in the file(s), so > need a way to load them dynamically, or load all the model classes.I''m not sure this is the preferred way, but if you load config/ environment.rb you will have access to (among other things) all of your models. So, for example, assuming your task was defined in lib/ tasks/somefile.rake you could call require thusly: require File.expand_path(File.dirname(__FILE__) + ''../../../config/ environment'') -John --~--~---------~--~----~------------~-------~--~----~ 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 Jun 7, 8:11 am, "Isak Hansen" <isak.han...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m working on a rake task that loads AR models from a yaml file, > which fails when I try to use a deserialized object, as its class > hasn''t been loaded. > > I don''t know beforehand exactly which classes are in the file(s), so > need a way to load them dynamically, or load all the model classes.I''m not sure this is the preferred way, but if you load config/ environment.rb you will have access to (among other things) all of your models. So, for example, assuming your task was defined in lib/ tasks/somefile.rake you could call require thusly: require File.expand_path(File.dirname(__FILE__) + ''../../../config/ environment'') -John --~--~---------~--~----~------------~-------~--~----~ 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 6/8/07, jparker <johncparker-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > On Jun 7, 8:11 am, "Isak Hansen" <isak.han...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > I''m working on a rake task that loads AR models from a yaml file, > > which fails when I try to use a deserialized object, as its class > > hasn''t been loaded. > > > > I don''t know beforehand exactly which classes are in the file(s), so > > need a way to load them dynamically, or load all the model classes. > > I''m not sure this is the preferred way, but if you load config/ > environment.rb you will have access to (among other things) all of > your models. So, for example, assuming your task was defined in lib/ > tasks/somefile.rake you could call require thusly: > > require File.expand_path(File.dirname(__FILE__) + ''../../../config/ > environment'') >I''ve made my task depend on the :environment task, which i believe takes care of this. I can use my models, but it looks like they are loaded lazily and aren''t available to YAML unless i explicitly use them beforehand. E.g.: # this works User.class.to_s # load the User class my_user = YAML.load(user_data) puts my_user.name # while this throws: undefined method `name'' for #<YAML::Object:0x343ed94> my_user = YAML.load(user_data) puts my_user.name Suggestions appreciated, Thanks, Isak> -John > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
It feels kludgy, but you could explicitly load the models ahead of
time:
task :preload => :environment do
# relative path assumes this is defined in a file in lib/tasks
Dir[File.dirname(__FILE__) + ''/../../app/models/*.rb''].each
do |
file|
require file
end
...
end
Alternatively - this feels even more kludgy - you could test whether
the class appears to be loaded and, if necessary, load the class and
reload the YAML data:
task :dynamic => :environment do
my_user = YAML.load(user_data)
unless my_user.respond_to?(:name)
Object.const_get(my_user.class)
my_user = YAML.load(user_data)
end
...
end
-John
On Jun 11, 1:16 am, "Isak Hansen"
<isak.han...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> On 6/8/07, jparker
<johncpar...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>
>
>
>
>
> > On Jun 7, 8:11 am, "Isak Hansen"
<isak.han...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> > > I''m working on a rake task that loads AR models from a
yaml file,
> > > which fails when I try to use a deserialized object, as its class
> > > hasn''t been loaded.
>
> > > I don''t know beforehand exactly which classes are in the
file(s), so
> > > need a way to load them dynamically, or load all the model
classes.
>
> > I''m not sure this is the preferred way, but if you load
config/
> > environment.rb you will have access to (among other things) all of
> > your models. So, for example, assuming your task was defined in lib/
> > tasks/somefile.rake you could call require thusly:
>
> > require File.expand_path(File.dirname(__FILE__) +
''../../../config/
> > environment'')
>
> I''ve made my task depend on the :environment task, which i believe
> takes care of this.
>
> I can use my models, but it looks like they are loaded lazily and
> aren''t available to YAML unless i explicitly use them beforehand.
>
> E.g.:
>
> # this works
> User.class.to_s # load the User class
> my_user = YAML.load(user_data)
> puts my_user.name
>
> # while this throws: undefined method `name'' for
#<YAML::Object:0x343ed94>
> my_user = YAML.load(user_data)
> puts my_user.name
>
> Suggestions appreciated,
>
> Thanks,
> Isak
>
> > -John
--~--~---------~--~----~------------~-------~--~----~
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 6/12/07, jparker <johncparker-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > It feels kludgy, but you could explicitly load the models ahead of > time: > > task :preload => :environment do > # relative path assumes this is defined in a file in lib/tasks > Dir[File.dirname(__FILE__) + ''/../../app/models/*.rb''].each do | > file| > require file > end > ... > end >Perhaps a bit kludgy yes, but it seems to work fine. Thanks! Isak --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---