An error with routes [ruby on rails] - ruby-on-rails-3

I have generated a form called Clients by using scaffold , then when I decided to change its url form routes file , I typed this command
match '/book', to: 'clients#new'
and now the problem is : I having two links for the new action , the first link is localhost:3000/clients/new , and the second one is localhost:3000/book , how to delete the first one ( clients/new ) , and thats also what I want to do for update / delete / index actions

in routes.rb file:
resources :clients, except: :new

Related

Preserve order in wordpress query

I'm makign one wordpress theme where I want to show more pages on home page. I made home.php which starts with this:
$args = array(
'post_type' => 'page',
'post__in' => explode(",",get_option('fp_pages')));
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ): $the_query->the_post();
....
I have one big problem. fp_pages has page ids divided with commas. I want them than to show on page in that order but result is different. Order of IDs doesn't matter - they are always the same, but I want it as I wrote it in fp_pages.
I did some research on web and found FIND_IN_SET(). My problem is that I don't know how to integrate it in wordpress query. Another possibility is to load them from database one by one but I'm afraid that it can slow down page loading (I'm talking about 8 - 10 pages max).
So can you help me with integrating FIND_IN_SET() to wordpress query or maybe tell if loading one by one is good idea or tell another solution :)
You need to have a look at the Order & Orderby Parameters of WP_Query. Since Wordpress 3.5, you have the option to preserve the order of posts by ID given in post__in
'post__in' - Preserve post ID order given in the post__in array (available with Version 3.5).
So you can simply add 'orderby' => 'post__in', to your query arguments

Easiest way to find a value in one of three columns?

I am using twilio to provide audio conference functionality in my rails app. When I call my conference number, twilio passes on a couple of values - including 'From' which contains the caller's phone number in international format.
I have a profile for every user in my system and in my controller I am querying the profile to provide a personalised welcome message. Every profile contains between 0 and 3 numbers (primary, secondary and cellphone) and I need to check the caller's ID against those three fields in all profiles.
When I use the console on my dev machine, the following code finds the correct profile:
Profile.find_by('+44000000000')
When I upload to heroku, I use following code instead:
name = Profile.find_by(params['From']) || 'there'
Which causes an error in my app:
2014-04-03T19:20:22.801284+00:00 app[web.1]: PG::DatatypeMismatch: ERROR: argument of WHERE must be type boolean, not type bigint
2014-04-03T19:20:22.801284+00:00 app[web.1]: LINE 1: SELECT "profiles".* FROM "profiles" WHERE (+4400000000) ...
Any suggestion how that could be solved?
Thanks!
Additional information:
I think my problem is that I don't know how to query either the whole profile or three columns at once. Right now the code:
name = Profile.find_by(params['From'])
is not correct (params['From'] contains a phone number) because I am not telling rails to query the columns primary phone number, secondary phone number and cellphone. Neither am I querying the whole profile which would also be an option.
So the question basically is:
How can I change this code:
Profile.find_by(params['From'])
so that it queries either all fields in all profiles or just the three columns with phone numbers which each profile contains?
Is there something like Profile.where(:primary_number).or.where(:secondary_number)or.where(:cellphone) => params['From']
?
I am not familiar with twilio and not sure if this helps but find and find_by_attribute_name accepts array of values as options:
name = Profile.find_by([params['From'], 'there'] )
suppose params['From'] was here , This should generate:
SELECT `profiles`.* FROM `profiles` WHERE `profiles`.`attribute` IN ('here', 'there')
Or:
If you are trying to build dynamic matcher at run time , which is called Meta-programming , you can try this code:
name = eval("Profile.find_by_#{params['From']) || 'there'}(#rest of query params here) ")
Update
First of all, i think you are not using find_by correctly!! the correct syntax is:
Model.find_by(attribute_name: value)
#e.g
Profile.find_by(phone_number: '0123456')
Which will call where and retrive one record, but passing a value will generate a condition that always passes, for example:
Model.find_by('wrong_condition')
#will generate SQL like:
SELECT `models`.* FROM `models` WHERE ('wrong_condition') LIMIT 1
#which will return the first record in the model since there is no valid condition here
Why don't you try:
Profile.where('primary_number = ? OR secondary_number = ? OR cellphone = ?', params['From'], params['From'], params['From'])
You can write your query like:
Profile.where("primary_number = ? or secondary_number = ? or cellphone = ?", params['From'])
Just double check the syntax, but that should do it.

Rails passing more than one parameters

Respected ppl ...
I have a grave design issue since many days ...kindly help out ...
Im coming to this stage from the previous screen after entering the hospital_id which u can c from the URL is 10 (using ransack) ... at this stage when i click "Add performance detail for employee" im passing only the employee_id to the performance creation screen ...
How do i capture the hospital_id from the previous screen and store in my performance creation screen ? ... (it has field hospital_id) ....
can i somehow use this raw sql query to store hospital_id directly ? ...
SELECT hospital_id
FROM employee_sanction_working
WHERE employee_id
Pls do help ...
Thnx
There are few ways you can resolve your problem:
1) you can pass hospital_id in parameter as Rubyman and Santhosh K have suggested Or
2) you can create a class variable in controller which you can use in your erb or haml view.
for example:
in controller:
def new
#hospital_id = params[:hospital_id] #this you can use in your view
end
in view
Hospital ID is <%= #hospital_id %>
Please refer this for more information
You can pass the hospital_id as parameter to "Add performance detail for employee" link.

How to format big int values in Activescaffold plugin rails 3

I am using rails 3 activesacaffold plugin for my admin pannel. I have a table which has big int values.
When I list the values its showing as '62,175,049,070'. How can I format this to show like '62175049070'. Since we are using activescaffold plugin we dont have any direct view pages to make modification.
Please help!
Use the delimiter option:
active scaffold :model do
...
conf.columns[:ndc].options = {:i18n_number => {:delimiter => ''}}
...
end
https://github.com/activescaffold/active_scaffold/wiki/API:-Column

Rails 3 where query

I am sending an array from jquery via the url requerts to the rails controller.
when I do this in my controller
log_array = (params[:log_ids])
logger.debug "This is the array #{log_array.to_a}"
I get this in my server log
This is the array 85,84,83,82
I am trying this query to get all the selected logs:
#logs = Log.where(['"logs"."id" IN (?)', log_array])
I get this on the server log
SELECT "logs".* FROM "logs" WHERE ("logs"."id" IN ('85,84,83,82'))
It sould be like this
SELECT "logs".* FROM "logs" WHERE ("logs"."id" IN (85,84,83,82))
It seems like it puts the arry in like a string.
Is there any way to make the sql right for an array?
You're making things too SQL-ish. Try this:
Log.find_all(params[:log_ids])
Or Log.where(:id => params[:log_ids]) if you want to use where() goodness.
Would use the .where listed below...check out the link to the rails 3.1 deprecations.
http://m.onkey.org/active-record-query-interface