views:

66

answers:

4
+1  Q: 

New to Rails Help

I have a database query that is returning to a variable which contains an address.

@terminal_address = Terminal.find(:all, :select => :full_address ,:conditions => ["id = ?" , params[:id]])

When I pass the @terminal_address to my geocoding code it errors because of how the data is being passed. If I replace the variable with an address in quotes it works fine. How can I extract the string from the variable?

This is very frustrating!

+4  A: 
@terminal_address = Terminal.find(:first, :conditions => ["id = ?" , params[:id]]).full_address

This should work.

What you were doing wrong:

  • You asked for :all instead of only :first of Terminals, you were given an array
  • Each entry in the array was a Terminal instance with only one attribute selected (full_address)
Bragi Ragnarson
Use find_by_id!
Ryan Bigg
A: 

The @terminal_address is an instance of the Terminal class. Thus, you will need to get an attribute of that class that contains the address information. In this case, the field appears to be named 'full_address' So something like:

Terminal.find(:first, :conditions => ["id = ?" , params[:id]]).full_address

should return the full address string

Rob Di Marco
Use find_by_id!
Ryan Bigg
+2  A: 

Uh, why not just:

Terminal.find_by_id!(params[:id], :select => :full_address).full_address

This will raise an error when the id cannot be found.

Ryan Bigg
ya, conditions with "id=?" are so not rails way
Leonid Shevtsov
A: 

Thank you for all of your help!

Cory Martin