PHP to RoR: What is equal of insert in RoR

In PHP from form

we get values in POST OR GET

than I use mysql_query(insert_value)

what is its equal in ROR

In PHP from form

we get values in POST OR GET

than I use mysql_query(insert_value)

what is its equal in ROR

-philip

Philip has given good links.

Basically all the database related stuffs are handled by the Active Record class which is one of the components of MVC(Model View Controller) architecture.

Rails handles the create record stuff the same way traditional programming languages such as C++ via new constructor.

If you want to add a new employee detail record the statement would be

emp1 = emp.new(:name=>“amala”, :website=>“http://www.tamil.net”, :email=>“a@b.com”) emp1.save

or

emp.new(:name=>“amala”, :website=>“http://www.tamil.net”, :email=>“a@b.com”).save

The fantastic feature of ROR is, mostly you dont need to know SQL query language, or you dont need to apply another Query language syntax.

The code becomes fantastically simple and readable from the functional point of view.

Thanks...

I try this and its work ...

<%

@obj= params[:category]

@catid=@obj["catid"]

@newrec= Category.new(:catid=>@catid) @newrec.save

%>

but when

i try to do this like this.. its not working ... reason it is different because ... I have only controller for "Category" and i have model for "contact" but not controller .. willl it work with one single controller or not

<%

@obj= params[:contact]

@name=@obj["name"]

@newrec= Contact.new(:name=>@name) @newrec.save

%>

Hm, why are you using <% %>? Is this code in your viewer?

Do you have a column “catid” for your model Category?

Could you please describe better what your models look like and what kind of relationship do they have between them?

Thanks,

Fernando Brito

There is no relationship between models. but i only have one controller and trying to control two models with it

it is possible..

Ali,

Before you are trying out in the view, use the console and try out that what you want to with the models. using ruby script/console in the project directory.

Then go step by step with the views.

Go through the tutorials first.

We can not teach you basics here.

Thanks buddy.

Ali Imran wrote:

<%

@obj= params[:category]

@catid=@obj["catid"]

@newrec= Category.new(:catid=>@catid) @newrec.save

%>

As far a I see it there are two primary patterns that you may not be accustomed to coming from the PHP world. Plus one principal you may be struggling against.

Patterns: 1. Model-View-Controller (MVC) 2. Object Relational Mapping (ORM).

1. Rails conforms, rather strictly, to the Model-View-Controller (MVC) design pattern. There are many articles available on MVC. Learn it, live it, & love it. Otherwise, you'll continue to struggle with Rails, and it will seem to fight against you.

2. ActiveRecord implements the Object Relational Mapping (ORM) design pattern. ORM is designed to abstract away SQL generation from higher level business logic. There are a few distinct advantages to this abstraction: a) Business and application logic are written a single language syntax, Ruby. b) SQL generation is handled by database adaptors, which hide (abstract) business logic from database implementation details. The underlying database can be easily replaced with an entirely different database engine without having to rewrite application and/or business logic.

Principals: 1. Convention Over Configuration

Convention Over Configuration was one of the primary principals that drew my interest to Ruby on Rails. For example, I notice that you mentioned a column named "catid." It appears that this column may be serving as the primary key of the "categories" table, which would be represented by the "Category" class.

The Rails convention would suggest that the primary key of this, and any other, table should be named "id." The idea put forth by Convention Over Configuration is to make certain "default" assumptions as follows: A model named "Category" would expect a database table named "categories" containing a primary key named "id." In nearly all cases these "defaults" can be overridden, but learning and following the convention can greatly ease the burden of development and provide consistency both within a project and across projects.

The sheer lack of convention, and the hundreds of configuration files that weigh down the development process of most Java based web frameworks led me to investigate alternative languages and frameworks for building web applications.

My discovery of Ruby on Rails was a delightful surprise. It is a framework that "thinks" the way I think. I was hooked from the start. Programming in Ruby reminded me of why I got into computer programming in the first place. I've had more fun learning Ruby on Rails, and developing applications with the framework than in the entirety of my relatively long Java career. I'm still mostly stuck in the Java world for my day job, but at least I can enjoy the joys of RoR for my personal projects.

I say all this for one primary reason, which is to say, "Approach learning Ruby on Rails on its terms, not on your own. Don't fight against its patterns simply because you did things a different way in other languages or frameworks." I hope you are soon able to embrace of beauty, consistency, and solid foundation that Ruby on Rails provides.

Good luck and have fun.

Thanks buddy, This is great help for just starter.

I spend a lot of time on just to understand this MVC. You are right this is a big difference between PHP and ROR.

Depends on whether you have used a FRAMEWORK in PHP before. PHP frameworks such as CakePHP, Symphony, etc. work very similarly to Rails. Ruby and PHP are both languages and they use the same concepts, with a different syntax, that’s all. But yes, if you’re used to just injecting SQL code into HTML pages and then rendering that out, the whole MVC thing will seem quite daunting, but it’s for the better.

Best regards

Peter De Berdt