我有三种模式:用户,公司和收入。我想加入公司和收入模型,以发布加入的模型结果。有人可以请我指出如何去加入这些表格并发布结果的正确方向吗?请注意,公司和收入模式可以通过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