Railsã®ARã§åã£ã¦ãããªãã¸ã§ã¯ãã«ã¢ã¯ã»ã¹ãã
èãããå½ããåãªãã ãã©
mysql> desc people; +---------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------------+--------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(255) | YES | | NULL | | | prefecture | varchar(255) | YES | | NULL | | | created_at | datetime | YES | | NULL | | | updated_at | datetime | YES | | NULL | | +---------------+--------------+------+-----+---------+----------------+ 7 rows in set (0.00 sec) mysql>
ã¿ãããªãã®ããã£ã¦Viewå´ã§
@prefecture_name = People.group(:prefecture)
ã¿ãããªæãã§prefectureãã¾ã¨ãã¦ããã¦
ãã«ãã¦ã³ã§prefectureãé¸æãã¦submitãã¦
<%= form_tag({:action=>"list"}, {:method=>"get"}) do %> # all_peoplesã¯peoples <%= select_tag 'prefecture', options_from_collection_for_select(@prefecture_name, "prefecture", "prefecture") %> <%= submit_tag 'subsubsub' %> <% end %>
ã§prefecture
ãparams
ã§åãåããããã«ãã¦ããã¦
def list @people_selected_by_prefecture = People.where(prefecture: params[:prefecture]) end
ã¿ãããªæãã§Controllerã§é¸æãããprefectureã§ã¬ã³ã¼ããæãã¦ããã¦
<% @people_selected_by_prefecture.each do |p| %> <%= p.name %> | <%= p.prefecture %><br> <% end %>
ã¿ãããªæãã§ãã«ãã¦ã³ã§é¸ãã prefectureã«è©²å½ãã人éãé¸ã¶
ã¿ãããªæã
<% @people_selected_by_prefecture.each do |p| %> <%= p[0] %> | <%= p[1] %><br> <% end %>
ã¨ãã§ãããã¨ãã¦ãããã©ããããHashã§ã¯ãªã