0

I am trying to create an empty model, populate it and then add it to the database.

I am looking through Google for the syntax for instantiating a simple model that I can set fields in, but there does not seem to be much documentation for that.

Is that just not the intended useage pattern? If it is, how can I just create the empty model?

Thanks!

7
  • 1
    Does your model already have a defined database structure? Commented May 16, 2012 at 19:21
  • 2
    We need more info. What do you mean by 'empty model'? One where there's no class defined for it at all, or something else... ? Commented May 16, 2012 at 19:23
  • What are you looking for beyond Blah.create(:name => 'bob') Commented May 16, 2012 at 19:24
  • @Casper no it doesn't, but I will need to define the db structure as I do it. It is mongo. Commented May 16, 2012 at 19:29
  • @x1a4 I meant something equivalent to Java's Obj x = new Obj(); Commented May 16, 2012 at 19:30

2 Answers 2

2

An ActiveRecord model works based on what fields it's related table has in your db. If you have no db yet you have no fields. The usage pattern goes like this:

$ rails g model client name:string
#stuff happens
$ rake db:migrate

You now have a model associated with a clients table that has a string attribute called name.

Now in your controller you can use this by

@client = Client.new
@client.name = "foo"
@client.save

Which will create the model object, set the name, and persist it to the db

Sign up to request clarification or add additional context in comments.

Comments

1

You should read up on the Rails Guides. Your current issue is covered at this link, but you really need to read up on getting started.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.