What if you don't want your model to be from Active Record?

31 views
Skip to first unread message

Peter

unread,
Oct 12, 2013, 6:06:21 PM10/12/13
to rubyonra...@googlegroups.com
Hello Everyone,

What if you don't want your model to be tied to a database therefore not ActiveRecord, where do you put that model instead?

I want to write a class that would wrap together the various functions from, let's say, the AWS API. Would I write that class and place it in the models directory? Or should I place it elsewhere? Thank you.

Peter

Jordon Bedwell

unread,
Oct 12, 2013, 6:49:59 PM10/12/13
to rubyonra...@googlegroups.com
On Sat, Oct 12, 2013 at 5:06 PM, Peter <pe...@poproj.com> wrote:
> What if you don't want your model to be tied to a database therefore not
> ActiveRecord, where do you put that model instead?

In models, because nobody said models had to be ActiveRecord because
models define behaviors but if you want the Railism that "models
should be ActiveRecord" kicked out of your models you should upgrade
to Rails 4 where now you can have ActiveRecord::Base and
ActiveModel::Model.

> I want to write a class that would wrap together the various functions from,
> let's say, the AWS API. Would I write that class and place it in the models
> directory? Or should I place it elsewhere? Thank you.

It depends on what you mean but "wrap together various functions".

Peter

unread,
Oct 12, 2013, 7:11:41 PM10/12/13
to rubyonra...@googlegroups.com
By "wrap together", I meant I would write a single function for all the functions I would use from the AWS API.

So, I should put this in the models directory, right? I want the controller to remain a "controller" with respect to rails-isms.

class MyAWSAPI
  def list_servers
    ... call to AWS API to get list of servers...
  end

  def show_server(int)
    ... call to AWS API to get info on server int ...
  end
end

Robert Walker

unread,
Oct 12, 2013, 8:09:51 PM10/12/13
to rubyonra...@googlegroups.com
Peter wrote in post #1124413:
> By "wrap together", I meant I would write a single function for all the
> functions I would use from the AWS API.
>
> So, I should put this in the models directory, right? I want the
> controller
> to remain a "controller" with respect to rails-isms.
>
> class MyAWSAPI
> def list_servers
> ... call to AWS API to get list of servers...
> end
>
> def show_server(int)
> ... call to AWS API to get info on server int ...
> end
> end

Yes, that would be a model object and models would be a good place to
put that. You might even consider implementing ActiveModel with your
class and gain some ActiveRecord like behavior, but not actually be an
ActiveRecord subclass. Depends on your needs.

--
Posted via http://www.ruby-forum.com/.
Reply all
Reply to author
Forward
0 new messages