2016-08-03 73 views
0

我有三種模式:用戶,公司和收入。我想加入公司和收入模型,以發佈加入的模型結果。有人可以請我指出如何去加入這些表格併發布結果的正確方向嗎?請注意,公司和收入模式可以通過unique_id號碼加入。下面是我的一些代碼:連接表rails postgresql

收入模式

class Revenue < ActiveRecord::Base 
    belongs_to :user 

    def self.import(file) 
    CSV.foreach(file.path, headers: true) do |row| 
    Revenue.create! row.to_hash 
    end 
    end 

end 

事務所型號

class Firm < ActiveRecord::Base 
    belongs_to :user 

    def self.import(file) 
    CSV.foreach(file.path, headers: true) do |row| 
    Firm.create! row.to_hash 
    end 
    end 

end 

用戶模型

類用戶<的ActiveRecord :: Base的

# Include default devise modules. Others available are: 
    # :confirmable, :lockable, :timeoutable and :omniauthable 
    before_save { self.email = email.downcase } 

    has_many :revenues 
    has_many :firms 


    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable, 
     :session_limitable, :confirmable 

    validates :name, :lastname, :industry, :company, :title, :address, :state, :city, :zip, presence: true 
    validates :phone, presence: true, length: { maximum: 11 } 


end 

收入DB

class CreateRevenues < ActiveRecord::Migration 
    def change 
    create_table :revenues do |t| 
     t.integer :unique_id 
     t.integer :revenue 
     t.integer :profit 
     t.references :user, index: true, foreign_key: true 

     t.timestamps null: false 
    end 
    end 
end 

事務所DB

class CreateFirms < ActiveRecord::Migration 
    def change 
    create_table :firms do |t| 
     t.integer :unique_id 
     t.string :name 
     t.string :state 
     t.string :city 
     t.references :user, index: true, foreign_key: true 

     t.timestamps null: false 
    end 
    end 
end 

查看

<h2>Firm Data</h2> 

    <body> 
    <table> 
     <tr> 
     <th>unique_id</th> 
     <th>name</th> 
     <th>state</th> 
     <th>city</th> 
    </tr> 

     <body> 
     <% @firms.each do |firm| %> 
     <tr> 
      <td><%= firm.unique_id %> </td> 
      <td><%= firm.name %> </td> 
      <td><%= firm.state %> </td> 
      <td><%= firm.city %> </td> 
     <tr> 
     <% end %> 
     </table> 
     </body> 

    <h2>Revenue Data</h2> 

    <body> 
    <table> 
     <tr> 
     <th>unique_id</th> 
     <th>revenue</th> 
     <th>profit</th> 
    </tr> 

     <body> 
     <% @revenues.each do |rev| %> 
     <tr> 
      <td><%= rev.unique_id %> </td> 
      <td><%= rev.revenue %> </td> 
      <td><%= rev.profit %> </td> 
     <tr> 
     <% end %> 
     </table> 
     </body> 
+0

嗨,歡迎來到Stack Overflow。在這裏,我們希望你自己去看看,然後向我們展示你的代碼(甚至是/尤其是它不工作)。即我們不會爲您編寫代碼,但如果您有足夠的時間瞭解,我們可以告訴您如何調整它以適應您的需求......在這種情況下,如果您向我們展示了您實際上需要(例如,你期望這些鏈接如何連接)......也許向我們展示一些關於鏈接後如何使用它們的僞代碼?但你肯定需要首先自己去關聯。 –

+0

您可能還會發現Rails關於關聯的指南會很有幫助:http://guides.rubyonrails.org/association_basics.html爲您提供Active Records可以相互鏈接的方式的良好基礎。我懷疑你可能對'has_many:through' –

+0

上的部分有興趣,你可以添加更多關於'Firm'和'Revenue'之間關係的細節嗎?它是1:1,1:n還是n:n關係? unique_id指的是什麼? – davideghz

回答

1

根據您的問題和意見,它看起來對我來說建立如下關係:

用戶has_many公司(公司)。公司has_on e收入。用戶has_many收入行。

# app/models/user.rb 
class User < ActiveRecord::Base 
    has_many :firms 
    has_many :revenues, through :firms 
end 

# app/models/firm.rb 
class Firm < ActiveRecord::Base 
    has_one :revenue 
end 

# app/models/revenue.rb 
class Revenue < ActiveRecord::Base 
    belongs_to :firm 
end 

不是存儲都firmsrevenues表中的一個unique_id的,這將是更好的收入使用foreign_key,像firm_id

相應遷移是:

class CreateFirm < ActiveRecord::Migration 
    def change 
    create_table :firm do |t| 
     t.string :name 
     t.string :state 
     t.string :city 

     t.timestamps null: false 
    end 
    end 
end 

class CreateRevenue < ActiveRecord::Migration 
    def change  
    create_table :firm do |t| 
     t.belongs_to :firm, index: true 
     t.integer :revenue 
     t.integer :profit 

     t.timestamps null: false 
    end 
    end 
end 

這將使您使用,例如,firm.revenue.profitapp/views/firms/show.html.erb視圖中顯示的profit值。

看看你的模型和遷移語法,你似乎沒有使用Rails 5.你可以找到關於has_one關係here的Rails 4.2文檔。

+0

感謝您的回覆,@davideghz。那麼收入的firm_id會與公司的unique_id一致嗎?在某些情況下,兩個表之間必須有一個共同的連接,這就是爲什麼我在每個表中放入一個唯一的ID。我是否正確理解這一點?對不起,我很感激你的耐心。我以前從未建立過多表格網站。注意到用戶不會創建數據也是很重要的。所有表格的數據將被上傳,不會被用戶更新或銷燬。 – richiepop2

+1

當您創建與Model關聯的表時,Rails將自動創建一個用作表的主鍵的「id」列。當您創建一個依賴模型(在這種情況下爲收入)並且您在遷移中添加't.belongs_to:firm,index:true'時,Rails將在作爲外鍵的收入表中添加一個'firm_id'。 請注意,我只是編輯我的答案,因爲有一個錯誤在遷移:) – davideghz