has_many / :through rails4

galettan

I've been back on rails after long period on other techno, and it appears i'm a bit rusty.

I've been trying to handle Project, Client and Contact and I'm a little confused by the results.

At first I had just one "contact_id" in my Project and everything was find, but I figured out I'd really love to have multiple contacts for one project.

So this is what i've been trying :

Project model:

class Project < ActiveRecord::Base
  belongs_to :owner, class_name: User, foreign_key: "owner_id"
  belongs_to :worker, class_name: User, foreign_key: "worker_id"
  #  belongs_to :contact, class_name: Contact, foreign_key: "contact_id"
  has_many :project_contacts
  has_many :contacts, :through => :project_contacts
  has_many :tasks
  has_many :feedbacks
  before_save :set_estimated_time
  #pleinty of utilities
end

Contact model:

class Contact < ActiveRecord::Base
  belongs_to :client, class_name: Client, foreign_key: "client_id"
  has_manny :project_contacts
  has_many :projects, :through => :project_contacts
end

ProjectContact model:

class ProjectContact < ActiveRecord::Base
  belongs_to :project
  belongs_to :contact
end

I thought it was ok but here's what I get when I use Rails console:

2.2.3 :001 > project = Project.find(31) Project Load (0.3ms) SELECT 'projects'.* FROM 'projects' WHERE 'projects'.'id' = 31 LIMIT 1 => # 2.2.3 :002 > project.contacts NameError: uninitialized constant Project::ProjectContact

Edit :

Contact migration

class AddProjectContacts < ActiveRecord::Migration
  def change
    create_table :project_contacts do |pc|
      pc.belongs_to :project, index: true
      pc.belongs_to :contact, index: true
      pc.timestamps null: false
    end
  end
end

I'd really appreciate any help with this

Bye

Richard Peck

For posterity, here's how I'd expect it to look:

#app/models/project.rb
class Project < ActiveRecord::Base
   has_many :project_contacts
   has_many :contacts, through: :project_contacts
end 

#app/models/project_contact.rb
class ProjectContact < ActiveRecord::Base
   #columns id | project_id | contact_id | created_at | updated_at
   belongs_to :project
   belongs_to :contact
end

#app/models/contact.rb
class Contact < ActiveRecord::Base
   has_many :project_contacts
   has_many :projects, through: :project_contacts
end

This should give you the ability to call:

@project = Project.find params[:id]
@project.contacts #-> shows all contacts

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Rails集合_选择Has_Many Through

来自分类Dev

has_many /:通过rails4

来自分类Dev

has_many /:通过rails4

来自分类Dev

rails has_many :through 或 has_many_belongs_to

来自分类Dev

Rails 4::has_many,:through和表名

来自分类Dev

Rails 4::has_many,:through和表名

来自分类Dev

Ruby on Rails'has_many:through',存储数据

来自分类Dev

Rails has_many:through和N + 1

来自分类Dev

在同一模型中的Rails关联“ has_many:through”

来自分类Dev

Rails在has_many:through关系中创建数据

来自分类Dev

Ruby on Rails'has_many:through',存储数据

来自分类Dev

Rails4 has_many通过参数

来自分类Dev

在rails_admin中为has_many:through关系使用rails 4中的orderable

来自分类Dev

在“ has_many:through”和“ has_and_belongs_to_many”之间进行选择的rails

来自分类Dev

Rails 4 ActsAsTenant复合体has_many through与设置的current_tenant不兼容

来自分类Dev

Rails4:使用has_many保存对象在has_many模型验证器上失败

来自分类Dev

如何在Rails中的同一模型(用户)之间建立has_many:through关系?

来自分类Dev

如何在实体框架中实现类似Rails的:has_many:through?

来自分类Dev

Rails,ActiveRecord,has_many:through:查找没有关联的所有记录

来自分类Dev

has_many中的Rails NameError未初始化常量:through关系

来自分类Dev

Rails Active Record,从has_many:through关系中获取相关记录,并与where子句关联

来自分类Dev

Rails使用has_many:through编辑联接表的其他字段

来自分类Dev

Rails通过`has_many:through`文本字段进行搜索

来自分类Dev

带有 has_many、through、class_name 和 where 子句的 Rails 模型关联

来自分类Dev

Rails 5 - has_many through: 和表单中的嵌套 fields_for

来自分类Dev

Rails 4 has_many:through关系:在从父模型创建动作时分配默认值以加入模型属性

来自分类Dev

Rails4:如何在许多has_many模型中显示数据

来自分类Dev

用has_many查找用户:through

来自分类Dev

Rails has_many链

Related 相关文章

  1. 1

    Rails集合_选择Has_Many Through

  2. 2

    has_many /:通过rails4

  3. 3

    has_many /:通过rails4

  4. 4

    rails has_many :through 或 has_many_belongs_to

  5. 5

    Rails 4::has_many,:through和表名

  6. 6

    Rails 4::has_many,:through和表名

  7. 7

    Ruby on Rails'has_many:through',存储数据

  8. 8

    Rails has_many:through和N + 1

  9. 9

    在同一模型中的Rails关联“ has_many:through”

  10. 10

    Rails在has_many:through关系中创建数据

  11. 11

    Ruby on Rails'has_many:through',存储数据

  12. 12

    Rails4 has_many通过参数

  13. 13

    在rails_admin中为has_many:through关系使用rails 4中的orderable

  14. 14

    在“ has_many:through”和“ has_and_belongs_to_many”之间进行选择的rails

  15. 15

    Rails 4 ActsAsTenant复合体has_many through与设置的current_tenant不兼容

  16. 16

    Rails4:使用has_many保存对象在has_many模型验证器上失败

  17. 17

    如何在Rails中的同一模型(用户)之间建立has_many:through关系?

  18. 18

    如何在实体框架中实现类似Rails的:has_many:through?

  19. 19

    Rails,ActiveRecord,has_many:through:查找没有关联的所有记录

  20. 20

    has_many中的Rails NameError未初始化常量:through关系

  21. 21

    Rails Active Record,从has_many:through关系中获取相关记录,并与where子句关联

  22. 22

    Rails使用has_many:through编辑联接表的其他字段

  23. 23

    Rails通过`has_many:through`文本字段进行搜索

  24. 24

    带有 has_many、through、class_name 和 where 子句的 Rails 模型关联

  25. 25

    Rails 5 - has_many through: 和表单中的嵌套 fields_for

  26. 26

    Rails 4 has_many:through关系:在从父模型创建动作时分配默认值以加入模型属性

  27. 27

    Rails4:如何在许多has_many模型中显示数据

  28. 28

    用has_many查找用户:through

  29. 29

    Rails has_many链

热门标签

归档