Rails 3 - Merge query options - ruby-on-rails-3

I have this method form a Rails 2.3.4 app:
def self.find_all_colored(query, options={})
finder_options = {:conditions => "color = #{query}"}.merge(options)
Car.find(:all, finder_options)
end
With which I can do:
Car.find_all_colored("red", :limit => 5)
But I am having a really bad time trying to get that to work in Rails 3.1.1, by now I can make it work but without the .merge(options), if I add that part:
def self.find_all_colored(query, options={})
Car.where("color = #{query}").merge(options)
end
I get this error:
undefined method `default_scoped?' for {:limit=>5}:Hash
I've googled and searched in stackoverflow.com but no luck...thanks!

Try the following:
def self.find_all_colored(query, options={})
self.all({:conditions => {:color => query}}.merge(options))
end

Related

Using Twilio in Rails 4 - Uninitialized Constant Error

I am trying to integrate Twilio with my Rails 4 app. I followed a tutorial, but I keep getting an error. Right now I am getting an Uninitialized Constant Error. I provided the code below. Thanks in advance.
Routes.rb
get '/share_over_sms' => 'listing_collections#share_over_sms'
Listing Collection Model
require "messenger"
def clean_number
client_number = self.client_number.scan(/\d+/).join
client_number[0] == "1" ? client_number[0] = '' : client_number
client_number unless client_number.length != 10
end
Messenger Module
module Messenger
def send_sms(number)
twilio_sid = "ENV['TWILIO_ACCOUNT_SID']"
twilio_token = "ENV['TWILIO_AUTH_TOKEN']"
#twilio_client = Twilio::REST::Client.new twilio_sid, twilio_token
from = '+1xxxxxxxxxx'
message = #twilio_client.account.sms.messages.create(
:from => from,
:to => "+1"+number,
:body => "This is a test message."
)
end
end
Listing Collections Controller
def share_over_sms
#client_phone = ClientPhone.new(listing_collection_params)
#client_phone.send_sms(#client_phone.clean_number)
redirect_to :back
end

Rails index action always returns null

So I am having an issue where I have this index action that is supposed to return json, but in the browser I am always getting a null response.
Controller
class Admin::Groups::UsersController < Admin::GroupsController
def index
#user_groups = Group.includes(:group_type, :users).where("group_types.group_type_name = ?", 'Users').order("groups.group_name ASC")
render json: #user_groups.to_json(:include => [:group_type, :users], :methods => :enabled)
end
def show
#user_group = Group.includes(:group_type, :users).where("group_types.group_type_name = ?", 'Users').find_by_id(params[:id])
render json: #user_group.to_json(:include => [:group_type, :users], :methods => :enabled)
end
end
Route file
namespace :admin do
namespace :groups do
resources :users, only: [:index, :show] do
get 'enabled', :on => :collection
get 'disabled', :on => :collection
end
end
end
Output of rake routes
admin_groups_users GET /admin/groups/users(.:format) admin/groups/users#index
What is really throwing me off is that when I run the same code that is inside the index action in the rails console, it is outputting exactly what I expected. Also the show action works fine. And just for testing, I added a new route that I just called index2 and copied + pasted the code from the index action into index2 and I got the correct output in the browser.
So it has to be something to do with my route, but I'm not sure what it is?
Edit 1: Add entire output of rake routes
enabled_admin_accounts GET /admin/accounts/enabled(.:format) admin/accounts#enabled
disabled_admin_accounts GET /admin/accounts/disabled(.:format) admin/accounts#disabled
account_owners_admin_accounts GET /admin/accounts/account_owners(.:format) admin/accounts#account_owners
admin_accounts GET /admin/accounts(.:format) admin/accounts#index
admin_account GET /admin/accounts/:id(.:format) admin/accounts#show
enabled_admin_users GET /admin/users/enabled(.:format) admin/users#enabled
disabled_admin_users GET /admin/users/disabled(.:format) admin/users#disabled
admin_users GET /admin/users(.:format) admin/users#index
admin_user GET /admin/users/:id(.:format) admin/users#show
enabled_admin_owners GET /admin/owners/enabled(.:format) admin/owners#enabled
disabled_admin_owners GET /admin/owners/disabled(.:format) admin/owners#disabled
currencies_admin_owner GET /admin/owners/:id/currencies(.:format) admin/owners#currencies
admin_owners GET /admin/owners(.:format) admin/owners#index
admin_owner GET /admin/owners/:id(.:format) admin/owners#show
enabled_admin_counterparties GET /admin/counterparties/enabled(.:format) admin/counterparties#enabled
disabled_admin_counterparties GET /admin/counterparties/disabled(.:format) admin/counterparties#disabled
admin_counterparties GET /admin/counterparties(.:format) admin/counterparties#index
admin_counterparty GET /admin/counterparties/:id(.:format) admin/counterparties#show
enabled_admin_venues GET /admin/venues/enabled(.:format) admin/venues#enabled
disabled_admin_venues GET /admin/venues/disabled(.:format) admin/venues#disabled
admin_venues GET /admin/venues(.:format) admin/venues#index
admin_venue GET /admin/venues/:id(.:format) admin/venues#show
enabled_admin_custody_banks GET /admin/custody_banks/enabled(.:format) admin/custody_banks#enabled
disabled_admin_custody_banks GET /admin/custody_banks/disabled(.:format) admin/custody_banks#disabled
admin_custody_banks GET /admin/custody_banks(.:format) admin/custody_banks#index
admin_custody_bank GET /admin/custody_banks/:id(.:format) admin/custody_banks#show
enabled_admin_client_currencies GET /admin/client_currencies/enabled(.:format) admin/client_currencies#enabled
disabled_admin_client_currencies GET /admin/client_currencies/disabled(.:format) admin/client_currencies#disabled
admin_client_currencies GET /admin/client_currencies(.:format) admin/client_currencies#index
admin_client_currency GET /admin/client_currencies/:id(.:format) admin/client_currencies#show
admin_currencies /admin/currencies(.:format) admin/client_currencies#index
admin_all_currencies /admin/all_currencies(.:format) admin/currencies#index
enabled_admin_groups GET /admin/groups/enabled(.:format) admin/groups#enabled
disabled_admin_groups GET /admin/groups/disabled(.:format) admin/groups#disabled
admin_groups GET /admin/groups(.:format) admin/groups#index
admin_group GET /admin/groups/:id(.:format) admin/groups#show
enabled_admin_groups_accounts GET /admin/groups/accounts/enabled(.:format) admin/groups/accounts#enabled
disabled_admin_groups_accounts GET /admin/groups/accounts/disabled(.:format) admin/groups/accounts#disabled
admin_groups_accounts GET /admin/groups/accounts(.:format) admin/groups/accounts#index
admin_groups_account GET /admin/groups/accounts/:id(.:format) admin/groups/accounts#show
enabled_admin_groups_account_owners GET /admin/groups/account_owners/enabled(.:format) admin/groups/account_owners#enabled
disabled_admin_groups_account_owners GET /admin/groups/account_owners/disabled(.:format) admin/groups/account_owners#disabled
admin_groups_account_owners GET /admin/groups/account_owners(.:format) admin/groups/account_owners#index
admin_groups_account_owner GET /admin/groups/account_owners/:id(.:format) admin/groups/account_owners#show
enabled_admin_groups_users GET /admin/groups/users/enabled(.:format) admin/groups/users#enabled
disabled_admin_groups_users GET /admin/groups/users/disabled(.:format) admin/groups/users#disabled
admin_groups_users GET /admin/groups/users(.:format) admin/groups/users#index
admin_groups_user GET /admin/groups/users/:id(.:format) admin/groups/users#show
enabled_admin_groups_counterparties GET /admin/groups/counterparties/enabled(.:format) admin/groups/counterparties#enabled
disabled_admin_groups_counterparties GET /admin/groups/counterparties/disabled(.:format) admin/groups/counterparties#disabled
admin_groups_counterparties GET /admin/groups/counterparties(.:format) admin/groups/counterparties#index
admin_groups_counterparty GET /admin/groups/counterparties/:id(.:format) admin/groups/counterparties#show
enabled_admin_groups_currencies GET /admin/groups/currencies/enabled(.:format) admin/groups/currencies#enabled
disabled_admin_groups_currencies GET /admin/groups/currencies/disabled(.:format) admin/groups/currencies#disabled
admin_groups_currencies GET /admin/groups/currencies(.:format) admin/groups/currencies#index
admin_groups_currency GET /admin/groups/currencies/:id(.:format) admin/groups/currencies#show
root / trades#index
jasminerice /jasmine Jasminerice::Engine
You need to move this route further down the route list.
admin_group GET /admin/groups/:id(.:format) admin/groups#show
The route list is searched in-order. /admin/groups/users is matching this route with :id equal to users.
render is either in the form
render :json => #user_groups
in which it calls to_json for you and it cannot pass arguments, or you can do this:
format.json { render #user_groups.to_json(:include => [:group_type, :users], :methods => :enabled) }
If you always want to return json no matter what format is, then just get rid of the json: thing:
render #user_groups.to_json(:include => [:group_type, :users], :methods => :enabled)
Please change your routes to this and check
namespace :admin do
namespace :groups do
resources :users, :only => [:index,:show],:collection => {:enabled => :get,:disabled => :get}
end
end

Ruby on Rails 3 - retrieved data always zero

I can't find a working solution for the following piece of code:
def search_last_rate
self.rate = Rate.find(:first, :select => "rateconverted", :conditions => ["dominant_id = ? and converted_id = ?", self.currency_bought_iso, self.currency_sold_iso], :order => 'ratedate, dateloaded, timeloaded DESC')
end
When I execute this in the console I retrieve the appropriate value:
`=> #<Rate rateconverted: 0.8131>`
All columns are defined in MYSQL2 as an decimal but when I'm using it in the model and new html page it's always saving the value 0.0.
Please advice :)
decimal and integer are different in mysql. if u want the data to be numeric then use 'integer' in migrations else 'float'. checkout Decimal VS Int in MySQL?
Make sure that self.rate is not an integer
Try
self.rate = Rate.find(:first, :select => "rateconverted", :conditions => ["dominant_id = ? and converted_id = ?", self.currency_bought_iso, self.currency_sold_iso], :order => 'ratedate, dateloaded, timeloaded DESC').to_f

Devise Gem and the Geocoder Gem

I am using geocoder and the devise gem. And i am trying to get coupons near user's location
Coupon Model
def index
if params[:search].present?
#coupons = Coupon.near([user_long, user_lat], 50, :order => :distance)
else
#coupons = Coupon.all
end
end
Application Helper
I have defined the user_long and user_lat
def user_long
current_user.longitude
end
def user_lat
current_user.latitude
end
Devise Gem
I have tried to use the devise gem helper to get the values like so
Coupon Model
def index
if params[:search].present?
#coupons = Coupon.near([current_user.longitude, current_user.latitude], 50, :order => :distance)
else
#coupons = Coupon.all
end
end
I am hitting the walls and celling with this. Can someone help out, i know this is newbie question for but i can't solve it so save my life?
You are not seeing anything on the index page because your #coupons array is empty:
#coupons = Coupon.near([current_user.longitude, current_user.latitude], 50, :order => :distance)
In development log (in the same window where rails server is running, if you are running the rails server from console), you should check out the SQL query generated for CouponsController#index action.
Assuming you defined your 'near' query like this:
class Coupon
scope :near, lambda { |longitude, latitude| some logic...
end
You can debug this 'near' method using "rails console" like this:
rails console
> Coupon.near(10, 20)
etc..
My mistake was that i that i had the longitude before that latitude, It was taking away the logic
This works.
def index
#coupons = Coupon.near([current_user.latitude, current_user.longitude], 50, :order => :distance)
end

Rspec testing complex code with undefined variables

Just need alittle bit of help with rspec testing... very new to it and dont quite know how to test this piece of code
# Get a list of tasks recently used by the user
recent_task_ids = Effort.all( :select => "project_task_id",
:conditions => { :user_id => #user_id },
:group => "project_task_id",
:order => "MAX( week_commencing )" )
# For each of the task ids, get the actual project task records
#recent_tasks = []
recent_task_ids.each do |effort|
if ProjectTask.find_by_id( effort.project_task_id ) != nil
#recent_tasks.push( ProjectTask.find( effort.project_task_id ) )
end
end
Not sure if your even supposed to test undefined variables this way but any help would be great
You can stub out the Effort.all method.
it "tests with nil values" do
Effort.stub(:all).and_return(nil)
end
Source: http://rspec.info/documentation/mocks/stubs.html and http://rspec.info/documentation/mocks/