You are here: Forums Ask a Rails expert Converting sql query to ruby o...
Posted in Forums : Ask a Rails expert
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. I’ve been trying the ruby code from the pastie Does anyone see what’s wrong? |
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]] |
Authority 0
Posting Rating 68
Sign in to rate this post
|
Is it safer to use this |
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:
I think you should then be able to do something like:
Check the API and test, sorry this is untested (tested now). Edit: tested now, works as I suggested. |
Authority 0
Posting Rating 68
Sign in to rate this post
|
Thanks Jason for your answer it works fine indeed! |
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. |
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