English 中文(简体)
Search logic functionality
原标题:

The functionality I m looking for: I have a form that will search my Proposal model. I want the form to contain a select box and display the categories I have. Now, category is merely a column in my Proposal Model, so the following code from railscasts does not work (as it assumes a category table and use of category_id)

            <%= f.collection_select :category_id_equals, Category.all, :id, :name, :include_blank => true %>

The closest thing I got was

<%= f.collection_select :category_equals, Proposal.category_like(""), :id, :category, :include_blank => true %>

This would work perfect if it only brought uniques from the category column, not repeating cases :-/.

Also...this leads me to question -- should I utilize category_id instead and create a categories table even though it would just have and id and name column?

Thanks in advance :-)

最佳回答

First off, yes you should put those categories in their own model. Or consider a plug-in like acts_as_taggable* A category is an entity in your application and should exists as such.

  1. Adding it as a model lets you easily force users to choose from the list and maintain the list.
  2. You can someday add a visible boolean if you want to hide some old categories from the view. You could just add a visible named_scope to easily query for the items to populate the collection_select with.
  3. Making the category a model will also let you know when each category was created.

If you want to hack together something with what you ve got, you could load up those objects, put their categories into an array and then do array.uniq! to get just the ones you want.

You could add a method to Proposal that returns the unique categories.

def self.unique_categories
  categories = []
  Proposal.all.each do |p|
    categories << p.category
  end
  categories.uniq
end
问题回答

暂无回答




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签