find_by in rails

Using find_by method, we can have Database interaction on easy as like other language. In rails there are different types of find mehtods, they are

ModelName.find()
ModelName.find_by_fieldname()
ModelName.find_by_sql()

1.find(value):
Used to find the particular the value with all the field in the row.
Example,
Signup.find(’12’)

2.find_by_fieldname(value):
Used to find the particular the value specified in fieldname on the table.

Example,
Signup.find_by_username(’12’)

3. find_by_sql():
This method will give the user to manage the query customization as the user like.

Example,
Signup.find_by_sql(“select * from signups where f.userid=’#{session[:user_id]}’ and s.id=f.followid limit 30”)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: