Home Calling models and their methods in external scripts
Reply: 1

Calling models and their methods in external scripts

goterpsgo
1#
goterpsgo Published in 2017-11-11 16:27:20Z

I'm trying to access an activerecord model using rails runner. I passed the following: rails runner "TabAccount.find_by_id(1).screen_name". I know this model is valid because I can use it and see the entry within my Rails app.

Ultimately I want to make a class that's a child of TabAccount and I'm getting hung up on that. I imagine the script would look something along the lines of this:

require Rails.root.join('app', 'models', 'tab_account.rb')
class TwitterAccount < TabAccount

  def initialize()
    @screen_name = TabAccount.find_by_id(1).screen_name
  end
end

Can I assume I'm pretty close and there's some nitpick detail I've overlooked?

goterpsgo
2#
goterpsgo Reply to 2017-11-11 16:54:23Z

For this specific problem what I was getting hung up on was to make sure I could access the model and apparently all I had to do to validate that was to put in rails r "puts TabAccount.find_by_id(1).screen_name". And then I do the same thing in my script to validate the model in that context.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.310106 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO