2016-08-03 264 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