简体   繁体   中英

Rails application planning, querying records by attribute?

In my rails application I have a Service model. The service model has a string attribute called :code .

In my application I want to be able to display all Service objects that have the same code.

For example, if several Service object records have a :code of 8H4 I want to be able to list out all of those Service records that have the :code 8H4.

I am going to need to be able to do this for many different codes.

How do I implement this? Do I need a different action in the controller for every code I want to use? If so, would this make the controller too fat? Thanks.

In your controller create an action called show (or anything you want):

@services = Service.where(:code => '8H4')

and then in your view/show.html.erb you can list all those services:

 <% @services.each do |t| %>
     <%= t.code %>
 <% end %>

You don't need a different action each different code, maybe a variable instead.

@services = Service.where(:code => your-variable-here)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM