2011-02-23 2 views
0

3 키 조인 테이블을 업데이트하는 동안 문제가 있습니다. 나는 꽤 오랫동안 이것을 해왔으며 그것을 고치는 방법을 모릅니다. 내가 중복을 얻을 수 있지만 (그래서 새로운 프로젝트가 잘 작동3 열 조인 테이블의 업데이트시 오류

has_and_belongs_to_many :users, 
    :join_table => "projects_roles_users", 
    :foreign_key => 'property_id', 
    :association_foreign_key => 'user_id', 
    :insert_sql => 'INSERT INTO 
       projects_roles_users(project_id,role_id,user_id) 
       VALUES(#{id}, #{role_ids}, #{user_ids})', 
    :delete_sql => 'DELETE FROM projects_roles_users 
        WHERE projects_roles_users.property_id = #{id}' 

    has_and_belongs_to_many :roles, 
    :join_table => "projects_roles_users", 
    :foreign_key => 'project_id', 
    :association_foreign_key => 'role_id', 
    :insert_sql => 'INSERT INTO 
       projects_roles_users(project_id,role_id,user_id) 
       VALUES(#{id}, #{role_ids}, #{user_ids})', 
    :delete_sql => 'DELETE FROM projects_roles_users 
        WHERE projects_roles_users.project_id = #{id}' 

: 즉, 새 프로젝트 작업을 얻을 수 있기를, 나는 (/models/project.rb에서)이 두 HABTM 선언을 설정했다 project_roles_users에서). 나는 사용자가 프로젝트에서이 역할을 업데이트 할 때, 나는이 오류가 : 여기

Mysql::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' 1414)' at line 3: INSERT INTO 
       projects_roles_users(project_id,role_id,user_id) 
       VALUES(29, ,) 

는 컨트롤러의 :

def update 
    @project = Project.find(params[:id]) 

    respond_to do |format| 
     if @project.update_attributes(params[:project]) 
     flash[:notice] = 'Project was successfully updated.' 
     format.html { redirect_to user_path(current_user.id) } 
     format.xml { head :ok } 
     else 
     format.html { render :action => "edit" } 
     format.xml { render :xml => @project.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 

그리고 여기 뷰의 :

<% form_for(@project,:html => { :multipart => true }) do |f| %> 
    <%= f.error_messages %> 

    <p> 
    <%= f.label :name %><br /> 
    <%= f.text_field :name %> 
    </p> 

    <p> 
    <% f.label :role, 'Role' %><br /> 
    <%= f.collection_select :role_ids, Role.find(:all, :order => 'position'), :id, :role, {}, :multiple => false %>  
    </p>   

    <%= f.hidden_field :user_ids, :value => current_user.id %> 

    <p> 
    <%= f.submit 'Update' %> 
    </p> 
<% end %> 

<%= link_to 'Show', @project %> | 
<%= link_to 'Back', projects_path %> 
<%= javascript_tag "$('address').focus()" %> 

마지막으로 다음은 로그입니다 (아마도 도움이 될 것입니다 ...) :

Processing ProjectsController#update (for 127.0.0.1 at 2011-02-23 08:43:29) [PUT] 
    Parameters: {"project"=>{"price"=>"12$", "city"=>"City", "address"=>"452 Street st.", "user_ids"=>"14", "role_ids"=>["2"], "postalcode"=>"12346", "description"=>"Description", "state"=>"State"}, "commit"=>"Update", "authenticity_token"=>"OKQaW4LPb3Xye6Kbh/W2EgxPWFe4aj26etIEVlCTKTg=", "id"=>"29"} 
    User Columns (0.9ms) SHOW FIELDS FROM `users` 
    User Load (0.1ms) SELECT * FROM `users` WHERE (`users`.`id` = 14) LIMIT 1 
    Page Load (0.1ms) SELECT * FROM `pages` WHERE (parent_id IS NULL) ORDER BY position 
    Project Columns (0.9ms) SHOW FIELDS FROM `projects` 
    Project Load (0.1ms) SELECT * FROM `projects` WHERE (`properties`.`id` = 29) 
    SQL (0.1ms) BEGIN 
    Role Columns (0.8ms) SHOW FIELDS FROM `roles` 
    Role Load (0.1ms) SELECT * FROM `roles` WHERE (`roles`.`id` = 2) 
    projects_roles_users Columns (0.5ms) SHOW FIELDS FROM `projects_roles_users` 
    Role Load (0.3ms) SELECT * FROM `roles` INNER JOIN `projects_roles_users` ON `roles`.id = `projects_roles_users`.role_id WHERE (`projects_roles_users`.project_id = 29) 
    User Load (0.3ms) SELECT `users`.id FROM `users` INNER JOIN `projects_roles_users` ON `users`.id = `projects_roles_users`.user_id WHERE (`projects_roles_users`.project_id = 29) 
    SQL (0.1ms) DELETE FROM projects_roles_users 
WHERE projects_roles_users.project_id = 29 
    User Load (0.2ms) SELECT `users`.id FROM `users` INNER JOIN `projects_roles_users` ON `users`.id = `properties_roles_users`.user_id WHERE (`projects_roles_users`.project_id = 29) 
    SQL (0.1ms) DELETE FROM projects_roles_users 
WHERE projects_roles_users.project_id = 29 
    SQL (0.5ms) describe `projects_roles_users` 
    User Load (0.3ms) SELECT `users`.id FROM `users` INNER JOIN `projects_roles_users` ON `users`.id = `projects_roles_users`.user_id WHERE (`projects_roles_users`.project_id = 29) 
    SQL (0.0ms) Mysql::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' 1414)' at line 3: INSERT INTO 
projects_roles_users(project_id,role_id,user_id) 
VALUES(29, ,) 
    SQL (0.1ms) ROLLBACK 

ActiveRecord::StatementInvalid (Mysql::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' 1414)' at line 3: INSERT INTO 
       projects_roles_users(project_id,role_id,user_id) 
       VALUES(29, ,)): 
    app/controllers/properties_controller.rb:71:in `update' 
    app/controllers/properties_controller.rb:70:in `update' 

Rendered rescues/_trace (136.5ms) 
Rendered rescues/_request_and_response (0.6ms) 
Rendering rescues/layout (internal_server_error) 
+0

모델에서'role_ids'와'user_ids'의 값을 얻지 못하고있는 것 같습니다. 이게 뭐야? 모델의 메소드입니까? (메소드가 있어야하며, 그렇지 않으면이 필드에 NoMethodError가 표시됩니다). 값은 'nil'이며 보간 (# {})하면 공백 ('')이됩니다. SQL에서 'VALUES (29,,)'를 확인하십시오. 여기서'nil' 값은 공백으로 재전송됩니다. – rubyprince

+0

그들은 뷰 (<% = f.collection_select : role_ids, Role.find (: all, : order => 'position'), : id, : role, {}, : multiple => false % > 및 <% = f.hidden_field : user_ids, : value => current_user.id %>). 모델에 두 가지 메소드를 추가해야한다고 생각합니다 (예 : def role_ids = (value) write_attribute : role_ids, (value? value.downcase : nil) end)? 나는 거기에 글을 써야할지 잘 모르겠다. – macro

답변

2

정말 HABTM을 더 이상 사용하고 싶지 않습니다. 본격적인 모델로 참여를 촉진하고 많은 연관을 통해 사용합니다. 세 가지 조인 테이블은 정말 미친 짓이다.

+0

나는 당신이 말하고있는 모델의 유형을 모델화하려고 노력했고 거기에 붙어있다 (만약 당신이 그것에 대해 생각해 보았다면) : http://stackoverflow.com/questions/5150031/linking-three-models- through-two-join-models – macro

관련 문제