2014-02-25 23 views
0

我有兩個表 - positions表,其中的字段爲id,position_id,first_namelast_name。另一張表是positionslist,字段爲id,positions。我將如何加入這兩張表? 這裏是我的positionscontroller:如何在rails上使用ruby連接兩個表

class PositionsController < ApplicationController 
    def index 
    #@positions = Position.all(:order=>'updated_at DESC') 
    @positions = Position.joins(:positions => :positionlists) 
    end 
    def new 
    @position = Position.new 
    end 

    def create 
    @position = Position.new(params.require(:position).permit(:position_id, :first_name, :last_name)) 
    if @position.save 
     redirect_to(:controller=>'positions') 
    else 
     render 'new' 
    end 
    end 

    def edit 
    @position = Position.find(params[:id]) 
    end 

    def update 
    @position = Position.find(params[:id]) 
    if @position.update(params.require(:position).permit(:position_id, :first_name, :last_name)) 
     redirect_to(:controller=>'positions') 
    else 
     render 'edit' 
    end 
    end 

    def destroy 
    @position = Position.find(params[:id]) 
    @position.destroy 
    redirect_to(:controller=>'positions') 
    end 
end 

我的模型position.rb

class Position < ActiveRecord::Base 
    has_many :positionlists 
    has_many :positionlists , :through => :positions 
    validates :first_name, length: { in: 4..20} 
    validates :last_name, length: { in: 4..20} 
end 

我的模型positionlists.rb

class Positionlists < ActiveRecord::Base 
    has_many :positions 
end 

任何我的志願服務崗位index.html.erb

<% content_for :title, "Positions" %> 
    <%= render "layouts/nav" %> 
    <div class="clear"></div> 
    <h1>Positions List</h1> 
    <%= link_to "Add", new_position_path, :class =>'btn btn-success' %> 
    <br /> 
    <br /> 
    <table class="table"> 
    <tr> 
     <th>First Name</th> 
     <th>Last Name</th> 
     <th>Position</th> 
     <th>Options</th> 
    </tr> 
    <% @positions.each do |positions| %> 

    <tr> 
     <td><%= positions.first_name %></td> 
     <td><%= positions.last_name %></td> 
     <td><%= positions.position_id %></td> 
     <td> 
     <%= link_to "Edit", edit_position_path(positions)%> | 
     <%= link_to "Delete", position_path(positions), 
           method: :delete, data:{confirm: 'Are you sure you want to delete this?.' }%> 
     </td> 
    </tr> 
    <% end %> 
    </table> 

非常感謝任何幫助!謝謝

+0

在'Positionlists.rb'模型中,你的意思是'belongs_to:position'?另請注意單數用法。它是':position'而不是':positions'。你能解釋你想做什麼嗎? –

+0

即時通訊試圖加入這兩個表中的表位置和表位置列表 – bobcatnyxz

+0

您是否使用導軌移動或手動創建這些表? –

回答

0

Rails爲您照顧這件事。

class PositionList < ActiveRecord::Base 
    has_many :positions 
end 

class Position < ActiveRecrod::Base 
    belongs_to :position_list 
end 

而且你position需要position_list_id列簡單。如果我正在理解你在這裏做什麼。

其他問題:命名您的類PositionLists而不是PositionList會混淆Rails automagic命名約定。

此外,您在表格中有一組奇怪的列。你想要的東西,只要我可以告訴你的職位表中需要有列:

first_name 
last_name 
id (automatic) 

你似乎沒有什麼在你的position_list表,所以沒有列,你需要有自動ID之外柱。在位置表中的Position類和position_list_id列中的belongs_to會自動關聯。

如果您想了解這一切可以嘗試運行自動腳手架,看看它是如何完成的,並從中學習。也許生成一個新的Rails應用程序,然後運行是這樣的:

rails g scaffold position_list name 
rails g scaffold position first_name last_name position_list:references 

這會給你position_list.positions可以爲任何position_list或Position.all所有位置的列表中的所有位置。

另外它聽起來像很多Rails對你來說是新的。哈特爾的經典是一個很好的介紹:http://ruby.railstutorial.org/ruby-on-rails-tutorial-book

+0

這就是我得到 協會名稱'位置'沒有找到位置;也許你拼錯了嗎? – bobcatnyxz

+0

你只是想要有兩個類 - 一個PositionList可以包含多個職位?從你的代碼我只是想看看你想實現什麼,所以我可以發佈你需要的代碼,因爲你在問題中發佈的代碼似乎存在很多問題......只是試圖磨合在正確的一個。 –

+0

誰刪除了提問者在編輯過程中明確需要的所有解釋? –