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 69
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 78
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 69
Sign in to rate this post

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

 
81539342a46008603039d00c8a7ab03d

Authority 62
Posting Rating 99
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 69
Sign in to rate this post

Thanks Jason for your answer it works fine indeed!

 
81539342a46008603039d00c8a7ab03d

Authority 62
Posting Rating 99
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 : nested application ApplicationController get called intead of children::ApplicationController

Ask a Rails expert : Best way to structure a database for a large/static dataset

Ask a Rails expert : Ruby Developer (ROR) - Scottish based (Remote working from within the UK)

Ask a Rails expert : Above Ground Pool Supplies

Ask a Rails expert : How to get url params in observer or model in Rails 3.1

Ask a Rails expert : What can persuade you to hire Junior Ruby devs with significant PHP experience?

Ask a Rails expert : What industry value does the Ruby or Rails Certification currently have?

Ask a Rails expert : Louis Vuitton Damier Azur Canvas specially sale ( www.salecheaplouisvuitton.com )

Ask a Rails expert : ·How to check errors/puts statements from ruby files which are under cronob

Ask a Rails expert : Louis Vuitton cheap Soft Sided Luggagespecial offer( www.salecheaplouisvuitton.com )

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