Keith Rumjahn
Keith Rumjahn

Reputation: 461

How to create a model in rails

I am using the parseresource gem and it says I need to create a model.

Create a model:

class Post < ParseResource
  fields :title, :author, :body

  validates_presence_of :title
end

I only know how to generate a mode and it always inherits ActiveRecord::Base. What should I type into my command line to create this model?

Upvotes: 2

Views: 11270

Answers (4)

Vasanth Saminathan
Vasanth Saminathan

Reputation: 585

rails generate model followed by your model name, then the fields with respective datatype and size.

Below is one exhaustive example.

rails generate model MyModel some_id:integer{20} some_name:string{255}
some_text:text some_int:integer{1} some_deci:decimal{10,2}

You can also have other data types such as boolean, date, time, datetime, float, binary.

Upvotes: 1

maecro
maecro

Reputation: 233

Unless the parseresource gem includes a custom generator, I think you will have to generate a standard ActiveRecord model and then edit it to inherit from ParseResource.

As another option you could just create the model from scratch by creating a post.rb file under app/models and putting your model code in there (any decent text editor will work just fine). Remember there is nothing forcing you to use a generator, they are just there to make your life easier.

Upvotes: 2

Robert French
Robert French

Reputation: 724

I think what you are looking for is:

rails generate model Post title:string author:string blob:text

Then change the inherits from ActiveRecord to inherits from ParseResource in the post.rb file created.

class Post < ActiveRecord::Base

becomes

class Post < ParseResource

I don't really have enough details about the model or ParseResource for a better answer. I do hope that helps though.

If you are new to Ruby and/or Rails I suggest running through an introduction to rails. http://guides.rubyonrails.org/getting_started.html or http://ruby.railstutorial.org/ruby-on-rails-tutorial-book

Cheers!

Upvotes: 6

Kevin Bedell
Kevin Bedell

Reputation: 13414

In this case it looks like you only need to create a single file in your /app/models directory named after the model you're trying to create.

The normal model generation code that comes with Rails and it's generators don't apply here since this gem is specifically intended to only serve as a 'wrapper' to the Parse.com API.

From the project's home page:

ParseResource makes it easy to interact with Parse.com's REST API. It adheres to the ActiveRecord pattern. ParceResource is fully ActiveModel compliant, meaning you can use validations and Rails forms.

It looks like all you do is create a single file descending from ParseResource and then interact with it as if it were a normal ActiveRecord model.

Looks pretty straightforward, though I'd caution that the author says right in the docs:

ParseResource is brand new. Test coverage is decent. This is my first gem. Be afraid.

Be careful and make sure you report any issues you find through the projects issues page in Github.

Upvotes: 4

Related Questions