开发者

Rails custom method

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 simp开发者_如何学运维ly 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!


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜