我有三種模式:用戶,公司和收入。我想加入公司和收入模型,以發佈加入的模型結果。有人可以請我指出如何去加入這些表格併發布結果的正確方向嗎?請注意,公司和收入模式可以通過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>
嗨,歡迎來到Stack Overflow。在這裏,我們希望你自己去看看,然後向我們展示你的代碼(甚至是/尤其是它不工作)。即我們不會爲您編寫代碼,但如果您有足夠的時間瞭解,我們可以告訴您如何調整它以適應您的需求......在這種情況下,如果您向我們展示了您實際上需要(例如,你期望這些鏈接如何連接)......也許向我們展示一些關於鏈接後如何使用它們的僞代碼?但你肯定需要首先自己去關聯。 –
您可能還會發現Rails關於關聯的指南會很有幫助:http://guides.rubyonrails.org/association_basics.html爲您提供Active Records可以相互鏈接的方式的良好基礎。我懷疑你可能對'has_many:through' –
上的部分有興趣,你可以添加更多關於'Firm'和'Revenue'之間關係的細節嗎?它是1:1,1:n還是n:n關係? unique_id指的是什麼? – davideghz