Browse the Ruby on Rails Community.

You are here: Forums Ask a Rails expert Converting sql query to ruby o...

Replytotopic

Converting sql query to ruby on rails query

Posted in Forums : Ask a Rails expert

 
Profile

Authority 0
Posting Rating 68
Sign in to rate this post

Hi there,

I’m trying to create a query in rails for showing specific content.
You can find the sql in the pastie.
But how can I implement this in rails?

I’ve been trying the ruby code from the pastie
But I’m having problems with my brackets.

Does anyone see what’s wrong?

http://pastie.org/380566

 
Photo#5117447231013780994

Authority 37
Posting Rating 77
Sign in to rate this post

I think that you need to use other brackets like:

Page.all :conditions => [“parent_id is null and content_id <> ’’ and category_id = ?”, params[:id]]

 
Profile

Authority 0
Posting Rating 68
Sign in to rate this post

Is it safer to use this
category_id = ?”, params[:id]]
or
find_all_by_category_id(params[:id])?

 
Wwr_me

Authority 62
Posting Rating 100
Sign in to rate this post

It’s not safer, just some people prefer using the dynamic helpers.

Something else you should look at is named_scope. This let’s you embed this sort of static condition in your model where it belongs.

I’m on my iphone so I can’t test this (tested now) but you would add something like this to your model:

named_scope :top_dutch, :conditions => "parent_id is null and dutch_content <> ''"

I think you should then be able to do something like:

Page.top_dutch.find_all_by_page_category_id(4)

Check the API and test, sorry this is untested (tested now).

Edit: tested now, works as I suggested.

 
Profile

Authority 0
Posting Rating 68
Sign in to rate this post

Thanks Jason for your answer it works fine indeed!

 
Wwr_me

Authority 62
Posting Rating 100
Sign in to rate this post

Happy to help, named_scope is an awesome feature for moving this sort of business logic back into the model.

Replytotopic

Other Recent Topics

Ask a Rails expert : How to Prevent double space being converted into "  " fckeditor

Ask a Rails expert : Random Issue with Invalid AuthenticityToken

Ask a Rails expert : Apache + Passenger -> RoR hates me!

Ask a Rails expert : which one i choose .net or ror

Ask a Rails expert : Tinymce 3.3.2

Ask a Rails expert : how do you organize javascript code in your rails project?

Ask a Rails expert : Custom Browse button in IE6(can not submit form)

Ask a Rails expert : gem install mysql issue

Ask a Rails expert : how to provide href in alert message

Ask a Rails expert : desert routes not running properly

Formatting Help
  • *bold*       _italics_      
    bq. (quotes)
  • "DSC":http://www.dsc.net
  • * or # (lists)
or cancel