rails - 选择了params params,但params“改变”仍然存在

时间:2016-04-01 01:52:11

标签: mysql ruby-on-rails ruby-on-rails-4 activerecord associations

我真的不确定如何描述这个问题,我之前从未遇到过这样的问题。我正在尝试建立一个“has_many:through”关联来处理对组的成员请求。其他行动(销毁,更新)似乎工作正常。每当我在控制器中启动创建操作时,对象都会显示正在传递正确的参数并创建关联。但是,它仅为具有“1”id的组创建关联。我不知道如何清楚地解释这个问题或解决它。我将在下面发布我的日志和代码。

澄清我的问题:为什么数据在持续存在于数据库(创建时)时会“改变”?

Cliqs =群组

日志:

Started POST "/cliqs/2/cliq_requests" for ::1 at 2016-03-31 20:35:32 -0500
Processing by CliqRequestsController#create as HTML
Parameters: {"authenticity_token"=>"uaVHFgB7digMywl2a/n2GKMtwi691WA/dw2F2mzdkSCK69C46TZICiSp90xldj3hosFwSOPEi3fSOvOSkIVMjA==", "cliq_id"=>"2"}
Cliq Load (0.0ms)  SELECT  `cliqs`.* FROM `cliqs` WHERE (2) LIMIT 1
User Load (0.0ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 1  ORDER BY `users`.`id` ASC LIMIT 1
(0.0ms)  BEGIN
SQL (1.0ms)  INSERT INTO `cliq_requests` (`cliq_id`, `user_id`, `created_at`, `updated_at`) VALUES (1, 1, '2016-04-01 01:35:32', '2016-04-01 01:35:32')
(198.0ms)  COMMIT
Redirected to http://localhost:3000/cliqs
Completed 302 Found in 237ms (ActiveRecord: 199.0ms)

控制器操作:

def create
    @cliq = Cliq.find_by(params[:cliq_id])
    @cliq_request = current_user.cliq_requests.new(cliq: @cliq)

    if @cliq_request.save
        redirect_to cliqs_path
    else
        redirect_to current_user
    end 
end

其他行动(以防万一):

def update
    @cliq = Cliq.find_by(params[:cliq_id])
    @cliq_request = CliqRequest.find(params[:id])
    @cliq_request.accept
end

def destroy
    @cliq = Cliq.find_by(params[:cliq_id])
    @cliq_request = CliqRequest.find(params[:id])
    @cliq_request.destroy
    if @cliq_request.destroy
        redirect_to cliqs_path
    else
        redirect_to current_user
    end
end

模特:

class User < ActiveRecord::Base
has_one :owned_cliq, foreign_key: 'owner_id', class_name: 'Cliq', dependent: :destroy

has_many :cliq_memberships, dependent: :destroy
has_many :cliqs, through: :cliq_memberships

has_many :cliq_requests, dependent: :destroy

end

class Cliq < ActiveRecord::Base
belongs_to :owner, class_name: 'User'

has_many :cliq_memberships, dependent: :destroy
has_many :members, through: :cliq_memberships, source: :user

has_many :cliq_requests, dependent: :destroy                                
has_many :pending_members, through: :cliq_requests, source: :user, foreign_key: 'user_id'
end

class CliqRequest < ActiveRecord::Base
#from
belongs_to :user
#to
belongs_to :cliq

def accept
    cliq.members << cliq.pending_members.find(user_id)
    destroy
end
end

最后我的观点:

<h1><%= @cliq.name %></h1>

<%= link_to 'Request to join Cliq', cliq_cliq_requests_path(@cliq, @cliq_request), :method => :post %>

<% @cliq_members.each do |cliq_member| %>
<ul><%= link_to cliq_member.username, user_path(cliq_member) %></ul>
<% end %>


<h3>Cliq Requests:</h3>

<ul>
<% @cliq.pending_members.each do |pending_member| %>
<%= link_to pending_member.username, user_path(pending_member) %>
<% end %>
<% @cliq.cliq_requests.each do |cliq_request| %>
<%= link_to "Accept", cliq_cliq_request_path(@cliq, cliq_request), :method => :put %>
<%= link_to "Deny", cliq_cliq_request_path(@cliq, cliq_request), :method => :delete %>
</ul>
<% end %>

1 个答案:

答案 0 :(得分:2)

正如您自己发现的那样,@cliq = Cliq.find_by(id: params[:cliq_id])使用了@cliq = Cliq.find_by(params[:cliq_id]),而find_by却没有。那么为什么会这样呢?

find_by方法符合条件。使用@cliq = Cliq.find_by(some_attribute: "foo"),您可以匹配任何属性。例如,这也可以:

find_by

因此,使用id时,必须指定find_by属性,以便查询返回正确的记录。您的SELECT `cliqs`.* FROM `cliqs` WHERE (2) LIMIT 1 查询实际上运行的SQL看起来像这样:

LIMIT 1

select语句将返回整个表,而@cliq只会获取第一条记录。

作为奖励,基于params[:cliq_id]分配find的首选导轨方式是仅使用@cliq = Cliq.find(params[:cliq_id]),使用其主键搜索记录:

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=strFile