Add field for an has_many association with :through

nobe4

I have a question about has_many associations :

Here is a sample of my db/models :

Table document

id   : int
name : text

class Document < ActiveRecord::Base
    has_many :document_pages
    has_many :pages, through: :document_pages
end

Table page

id      : int
content : text

class Page < ActiveRecord::Base
    has_many :document_pages
    has_many :documents, through: :document_pages
end

Table document_pages

document_id : int
page_id     : int
page_number : int

class Page < ActiveRecord::Base
    belongs_to :pages
    belongs_to :documents
end

I am creating the page and the document and I link the two this way :

page = Page.create(:content => 'lorem')
document = Document.where(:id => id).first_or_initialize.tap do |document|
    document.pages << page unless document.pages.include?(page)
end

And in the line document.pages << page I want to provide the field page_number of the table document_pages.

Do you know how to do it ?

Arup Rakshit

Do it differently :

page = Page.create(:content => 'lorem')

document = Document.where(:id => id).first_or_initialize. do |document|
    document.document_pages.build(page: page, page_number: 1 ) unless document.pages.include?(page)
    document.save!
end

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

The has_many :through Association

From Dev

Querying double has_many :through association

From Dev

The has_many :through Association - Modification?

From Dev

rails scope through has_many association

From Dev

Can a has_many through association be polymorphic?

From Dev

Rails has_many :through association

From Dev

rails scope through has_many association

From Dev

Can a has_many through association be polymorphic?

From Dev

Rails delete has_many through association

From Dev

Querying double has_many :through association

From Dev

The has_many :through Association - Modification?

From Dev

Has_many through association - View? Controller?

From Dev

Rails has_many through association issue

From Dev

ActiveRecord how to add existing record to association in has_many :through relationship in rails?

From Dev

Active Record how to add records to has_many :through association in rails

From Dev

Rails: Add attribute values to several new records in a join table in a has_many :through association

From Dev

"has_many :through" association through a polymorphic association with STI

From Dev

how to add has_many association?

From Dev

many-to-many with has_many :through association nested form

From Dev

Rails 4: combining has_many :through association with polymorphic association

From Dev

How to create a 'has_many through' association if a polymorphic association is involved?

From Dev

Rails 4: Delete on has_many through association

From Dev

Rails/ActiveRecord has_many through: association on unsaved objects

From Dev

Rails, How to delete an association in has_many through

From Dev

Weird issue has_many through association in updated Rails

From Dev

Rails has_many :through association: save instance into join table

From Dev

Rails 4: CanCanCan abilities with has_many :through association

From Dev

Rails 4: routing for has_many :through association

From Dev

on a has_many :through association, replacing conditions with lambda

Related Related

  1. 1

    The has_many :through Association

  2. 2

    Querying double has_many :through association

  3. 3

    The has_many :through Association - Modification?

  4. 4

    rails scope through has_many association

  5. 5

    Can a has_many through association be polymorphic?

  6. 6

    Rails has_many :through association

  7. 7

    rails scope through has_many association

  8. 8

    Can a has_many through association be polymorphic?

  9. 9

    Rails delete has_many through association

  10. 10

    Querying double has_many :through association

  11. 11

    The has_many :through Association - Modification?

  12. 12

    Has_many through association - View? Controller?

  13. 13

    Rails has_many through association issue

  14. 14

    ActiveRecord how to add existing record to association in has_many :through relationship in rails?

  15. 15

    Active Record how to add records to has_many :through association in rails

  16. 16

    Rails: Add attribute values to several new records in a join table in a has_many :through association

  17. 17

    "has_many :through" association through a polymorphic association with STI

  18. 18

    how to add has_many association?

  19. 19

    many-to-many with has_many :through association nested form

  20. 20

    Rails 4: combining has_many :through association with polymorphic association

  21. 21

    How to create a 'has_many through' association if a polymorphic association is involved?

  22. 22

    Rails 4: Delete on has_many through association

  23. 23

    Rails/ActiveRecord has_many through: association on unsaved objects

  24. 24

    Rails, How to delete an association in has_many through

  25. 25

    Weird issue has_many through association in updated Rails

  26. 26

    Rails has_many :through association: save instance into join table

  27. 27

    Rails 4: CanCanCan abilities with has_many :through association

  28. 28

    Rails 4: routing for has_many :through association

  29. 29

    on a has_many :through association, replacing conditions with lambda

HotTag

Archive