Were to put a method

i have this in my view, http://pastie.caboo.se/175030
but it doesnt seem to work.

im not sure what im doing wrong… it think i have the wrong syntax. i
get
“no block given” error on line 5

And what would be the better way to do this? i tried making a method
“find_by(field, item)” but it didnt work out…

View this message in context:
http://www.nabble.com/were-to-put-a-method-tp16480986p16480986.html
Sent from the RubyOnRails Users mailing list archive at Nabble.com.

Try this: (Assuming you have a Model defined as: Prop
and that model has an attribute called “category”:

<% Prop.find_all_by_category(“car”).each do |prop| %>

  •  <%=h prop.title %>
  • <% end %>

    -Danimal

    Well i tried what you said, and it worked.
    one thing though. I have multiple items in my category column in the
    db, seperated by spaces… and this is what i tried, with no luck if
    there are more than one item.

    def find_all_by_category(item)
    self.find(:all, :params => {:category => /#{item}/})
    end

    Just incase anyone was wondering, this is what i came up with after
    more reading of the api and spending some time on freenode, hope it
    helps you.
    def self.find_all_by_category(item)
    self.find( :all, :conditions => [“category like ?”, “%#{item}%”] )
    end

    Ilya,

    That looks good! Glad you got it working. Basically, it’s a way to do
    matches on substrings of category. I.e. if I have a category of “Car”
    I could do: Prop.find_all_by_category(“ar”) and it would match “Car”
    and anything else like that.

    The one thing I’d caution you on: this may end up being confusing in
    the future. “find_all_by_X” (where X is some attribute on that model)
    is already part of AR associations. So you are redefining an existing
    method to work differently, which may confuse you or mess you up
    later. My recommendation is to call it something differently, maybe
    like: find_by_category_substring(item).

    Just a suggestion though. :slight_smile:

    -Danimal

    This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

    | Privacy Policy | Terms of Service | Remote Ruby Jobs