Ruby on Rails:按属性而不是 id 查找记录
Ruby on Rails: Find a record by an attribute not an id
我是 rails 的新手,所以请耐心等待。
简而言之,我正在尝试创建一个表格,参加婚礼的客人可以在其中输入一个简单的代码 (invite_code
),然后回复。 from 应该采用 invite_code
然后直接采用 use 到正确的 invites#show
视图。
到目前为止一切顺利,但我无法尝试让 rails 通过 id
以外的其他方式查找记录,我想通过 invite_code
查找。假设我有一个 Invite
,id
为 4,invite_id
为 1234,当我在 from 中输入“4”而不是“1234”时,表单找到了正确的记录'.这里有一些代码来解释:
routes.rb
get 'invites/search', to: 'invites#show', controller: :invites
形式
...
<%= form_tag invites_search_path, method: :get do %>
<%= label_tag :invite_code, "#" %>
<%= text_field_tag :invite_code, nil %>
<%= submit_tag "Search", name: nil %>
<% end %>
...
invites_controller
...
def show
if params.has_key?(:invite_code)
@invite = Invite.find(params[:invite_code])
else
@invite = Invite.find(params[:id])
end
end
...
rake 路由输出
Prefix Verb URI Pattern Controller#Action
info_index GET /info/index(.:format) info#index
invites GET /invites(.:format) invites#index
POST /invites(.:format) invites#create
new_invite GET /invites/new(.:format) invites#new
edit_invite GET /invites/:id/edit(.:format) invites#edit
invite GET /invites/:id(.:format) invites#show
PATCH /invites/:id(.:format) invites#update
PUT /invites/:id(.:format) invites#update
DELETE /invites/:id(.:format) invites#destroy
invites_search GET /invites/search(.:format) 邀请#show
root GET / info#index
URL 示例
.../invites/search?utf8=%E2%9C%93&invite_code=1234
"utf8"=>"✓", "invite_code"=>"1234", "id"=>"search"
应用程序似乎忽略了控制器中 if 语句的 invite_id
部分...
感谢任何帮助,我花了很长时间才走到这一步...
find
默认使用id
字段,使用where
代替
if params.has_key?(:invite_code)
@invite = Invite.where(invite_code: params[:invite_code]).first
...
def show
if params.has_key?(:invite_code)
@invite = Invite.find_by(invite_code: params[:invite_code])
# find_by argument: value
# returns first match or nil
# same as find, where find searches by id
# Invite.find_by_invite_code params[:invite_code] is deprecated
else
@invite = Invite.find params[:id]
end
end
...
你有几个选择。 find_by_invite_code
将 return 你第一场比赛:
Invite.find_by_invite_code(params[:invite_code]) # First match or nil
虽然 where
会给你所有的匹配项作为数组
Invite.where(invite_code: params[:invite_code]) # Array of matches. May be empty
您还可以对 find_by
使用以下语法:
Invite.find_by(invite_code: params[:invite_code]) # First match or nil
我是 rails 的新手,所以请耐心等待。
简而言之,我正在尝试创建一个表格,参加婚礼的客人可以在其中输入一个简单的代码 (invite_code
),然后回复。 from 应该采用 invite_code
然后直接采用 use 到正确的 invites#show
视图。
到目前为止一切顺利,但我无法尝试让 rails 通过 id
以外的其他方式查找记录,我想通过 invite_code
查找。假设我有一个 Invite
,id
为 4,invite_id
为 1234,当我在 from 中输入“4”而不是“1234”时,表单找到了正确的记录'.这里有一些代码来解释:
routes.rb
get 'invites/search', to: 'invites#show', controller: :invites
形式
...
<%= form_tag invites_search_path, method: :get do %>
<%= label_tag :invite_code, "#" %>
<%= text_field_tag :invite_code, nil %>
<%= submit_tag "Search", name: nil %>
<% end %>
...
invites_controller
...
def show
if params.has_key?(:invite_code)
@invite = Invite.find(params[:invite_code])
else
@invite = Invite.find(params[:id])
end
end
...
rake 路由输出
Prefix Verb URI Pattern Controller#Action
info_index GET /info/index(.:format) info#index
invites GET /invites(.:format) invites#index
POST /invites(.:format) invites#create
new_invite GET /invites/new(.:format) invites#new
edit_invite GET /invites/:id/edit(.:format) invites#edit
invite GET /invites/:id(.:format) invites#show
PATCH /invites/:id(.:format) invites#update
PUT /invites/:id(.:format) invites#update
DELETE /invites/:id(.:format) invites#destroy
invites_search GET /invites/search(.:format) 邀请#show root GET / info#index
URL 示例
.../invites/search?utf8=%E2%9C%93&invite_code=1234
"utf8"=>"✓", "invite_code"=>"1234", "id"=>"search"
应用程序似乎忽略了控制器中 if 语句的 invite_id
部分...
感谢任何帮助,我花了很长时间才走到这一步...
find
默认使用id
字段,使用where
代替
if params.has_key?(:invite_code)
@invite = Invite.where(invite_code: params[:invite_code]).first
...
def show
if params.has_key?(:invite_code)
@invite = Invite.find_by(invite_code: params[:invite_code])
# find_by argument: value
# returns first match or nil
# same as find, where find searches by id
# Invite.find_by_invite_code params[:invite_code] is deprecated
else
@invite = Invite.find params[:id]
end
end
...
你有几个选择。 find_by_invite_code
将 return 你第一场比赛:
Invite.find_by_invite_code(params[:invite_code]) # First match or nil
虽然 where
会给你所有的匹配项作为数组
Invite.where(invite_code: params[:invite_code]) # Array of matches. May be empty
您还可以对 find_by
使用以下语法:
Invite.find_by(invite_code: params[:invite_code]) # First match or nil