Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-4453

Ignore attr_readonly when defined on foreign key fields

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Minor - P4 Minor - P4
    • 7.0.0, 6.2.1
    • Affects Version/s: 6.2.0
    • Component/s: None
    • Labels:
      None

      ActiveRecord ignores it:

      class Child < ApplicationRecord
        attr_readonly :mother_id
        belongs_to :mother
      end
      
      class Mother < ApplicationRecord
        has_many :children
      end
      
      2.4.1 :001 > c = Child.new
       => #<Child id: nil, mother_id: nil, created_at: nil, updated_at: nil, name: nil> 
      2.4.1 :002 > m = Mother.new
       => #<Mother id: nil, created_at: nil, updated_at: nil> 
      2.4.1 :003 > c.mother = m
       => #<Mother id: nil, created_at: nil, updated_at: nil> 
      2.4.1 :004 > c.save
         (0.1ms)  begin transaction
        SQL (0.4ms)  INSERT INTO "mothers" ("created_at", "updated_at") VALUES (?, ?)  [["created_at", "2017-06-30 05:47:37.580014"], ["updated_at", "2017-06-30 05:47:37.580014"]]
        SQL (0.2ms)  INSERT INTO "children" ("mother_id", "created_at", "updated_at") VALUES (?, ?, ?)  [["mother_id", 9], ["created_at", "2017-06-30 05:47:37.589372"], ["updated_at", "2017-06-30 05:47:37.589372"]]
         (1.4ms)  commit transaction
       => true 
      2.4.1 :005 > c.id
       => 6 
      2.4.1 :006 > m.id
       => 9 
      2.4.1 :007 > m2 = Mother.new
       => #<Mother id: nil, created_at: nil, updated_at: nil> 
      2.4.1 :008 > m2.save
         (0.1ms)  begin transaction
        SQL (2.4ms)  INSERT INTO "mothers" ("created_at", "updated_at") VALUES (?, ?)  [["created_at", "2017-06-30 05:47:46.046904"], ["updated_at", "2017-06-30 05:47:46.046904"]]
         (1.4ms)  commit transaction
       => true 
      2.4.1 :009 > m2.id
       => 10 
      2.4.1 :010 > c.mother = m2
       => #<Mother id: 10, created_at: "2017-06-30 05:47:46", updated_at: "2017-06-30 05:47:46"> 
      2.4.1 :012 > c.save
         (0.1ms)  begin transaction
        SQL (0.5ms)  UPDATE "children" SET "mother_id" = ?, "updated_at" = ? WHERE "children"."id" = ?  [["mother_id", 10], ["updated_at", "2017-06-30 05:47:54.513353"], ["id", 6]]
         (1.5ms)  commit transaction
       => true 
      2.4.1 :013 > c.reload
        Child Load (0.3ms)  SELECT  "children".* FROM "children" WHERE "children"."id" = ? LIMIT ?  [["id", 6], ["LIMIT", 1]]
       => #<Child id: 6, mother_id: 10, created_at: "2017-06-30 05:47:37", updated_at: "2017-06-30 05:47:54", name: nil> 
      2.4.1 :014 > c.mother.id
        Mother Load (0.3ms)  SELECT  "mothers".* FROM "mothers" WHERE "mothers"."id" = ? LIMIT ?  [["id", 10], ["LIMIT", 1]]
       => 10
      
      

            Assignee:
            emily.stolfo Emily Stolfo
            Reporter:
            emily.stolfo Emily Stolfo
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: