0

I have been looking online about how to do queries for Ruby on Rails and I can find information how to build the query but not where I put the physical code. I am very new to Ruby on Rails.

I have one database called story and it has the title and content columns that I made. I have a page that is used to input the title and content and then stored into the database and then I have a page which displays all contents of the database but I do not understand how to do a query to find let's say, "a record that has a content or title that contains a certain word or phrase"

   <%= @story.content %>

So i understand that displays the content on the screen, do i just do the same thing for queries?

2
  • 1
    Have you created any controllers? You can't (shouldn't) execute SQL right on the page like some do in PHP. Commented Apr 25, 2013 at 15:10
  • woz is correct. your queries belong in your controllers, which expose the data then displayed in your views. Commented Apr 25, 2013 at 15:21

2 Answers 2

3

In rails active records are used not direct sql queries

here's a good link that explains it ......

http://guides.rubyonrails.org/active_record_querying.html

For example lets say you have post model and you want

  1. Index all posts then use

    @posts = Post.all

  2. you want to show specific post ,find it by id

    @post = Post.find(params[:id])

  3. create a new post by

    @post = Post.new

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

2 Comments

under my app/models I have a story.rb and it contains the story class which inherits from ActiveRecord. You said I need to do @story = Story.all but where do I put that? Do I put that in the view of the page so it gets posted on the page? Or do I put it in the class? I don't understand how I get the query displayed on the page
You have to put '@story = Story.all' in your controllers index action and call '@story' in view to index stories
1

As per your query, In your controller Write like

@stories = Story.where("stories.content IS NOT NULL")  #Records those has a content
@stories = Story.where("stories.title LIKE%a_certain_word_or_phrase%") # Records that contains a certain word or phrase

If you want to pick up only one recoed from the databse you can user .first .last for example

@sotory = Story.where("stories.content IS NOT NULL").first #first item which has content
@sotory = Story.where("stories.content IS NOT NULL").first #last item which has content

You can also find by id like

@story = Story.find(1) # finds the record with id = 1

and so on ......

I think you need a good tutorial.As you are a newbie please checkout Ruby on Rails Guides: Active Record Query Interface

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.