[Rails] How avoid concurrency belongs_to and has_many association?

20 views
Skip to first unread message

Ricardo do Valle

unread,
Aug 9, 2014, 3:23:39 AM8/9/14
to rubyonra...@googlegroups.com

How do I avoid concurrency and keep the data integrity if one user submit a form before the other?

My issue is that the last user to submit the person/_form.rb will get all chosen cards, same the cards that first user chose and I want to prohibit that.

    # person.rb
    class Person < ActiveRecord::Base
      has_many :cards
    end

    # card.rb
    class Card < ActiveRecord::Base
      belongs_to :person
      scope :not_assigned_to_a_person, -> { Card.where(person_id: nil) }
    end

    # create_cards.rb
    create_table :cards do |column|
      column.references :person, index: true
    end

    # person/_form.rb
    = f.input :card_ids,
        collection: Card.not_assigned_to_a_person,
        label_method: :name, value_method: :id,
        input_html: { multiple: true }

StackOverflow: http://stackoverflow.com/questions/25216219/how-do-i-avoid-concurrency-belongs-to-and-has-many-association

--
Ricardo do Valle

Walter Lee Davis

unread,
Aug 9, 2014, 8:59:20 AM8/9/14
to rubyonra...@googlegroups.com
Have a look back into the mists of time. This may still be useful.

http://railscasts.com/episodes/59-optimistic-locking

Walter

> ​
> --
> Ricardo do Valle
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CALQXn36%3D2PZ%2BGJbQD0shL8pRXWqnoHtMKSJsXGCcHR86uK1cmw%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.

Ricardo do Valle

unread,
Aug 10, 2014, 8:58:41 PM8/10/14
to rubyonra...@googlegroups.com

Hi Walter,

I think that is not the case lock_version because my problem already exists on creation of two different Person which must includeCards (has_many) for these Person. My problem is: shouldn’t include Cards that have been associated.

I am not certainly, but I think the only way it is creating index or constrains on database tables.

Thanks,




For more options, visit https://groups.google.com/d/optout.



--
Ricardo do Valle
Reply all
Reply to author
Forward
0 new messages