views:

37

answers:

1

Hi,

I have a Place model that has both 'city_name' and 'name' as attributes. I would like to define a custom method that finds the place whose name matches the city_name for another place eg.

Place.name = "foo"
Place.city_name = "baz"

then Place.find_city gives the record where Place.name = "baz". At the moment I've got something along the lines of:

def find_city
  Place.find_by_name("this.place.city_name")
end

View:

<%= link_to "#{@place.city_name}", place_path(@place.find_city) %>

This code currently doesn't throw up any errors, but the link simply returns the current place record. Is this approach possible, and if so, what would be the best way to go about doing it? Thanks in advance!

A: 

Try something like this (assuming this method is part of model)

def find_city
  Place.find_by_name(city)
  # param should be either 'city' or 'city_name',
  # I'm confused by your attribute naming
end
Nikita Rybak
Sorry, my mistake - should have read Place.city_name = "baz". I've updated my question.
Sonia
@Sonia does it work?
Nikita Rybak
@Nikita Rybak brilliant, it does work, thanks!
Sonia