Ruby on rails Rails ActiveRecord电铲(<;<;)操作员

Ruby on rails Rails ActiveRecord电铲(<;<;)操作员,ruby-on-rails,ruby,ruby-on-rails-4,activerecord,ruby-on-rails-5,Ruby On Rails,Ruby,Ruby On Rails 4,Activerecord,Ruby On Rails 5,因此,我的应用程序中有一些代码附加到一个与“有很多关系的文件中,当您使用铲操作符(收集)时,我知道,我在搜索“实际上,只有当“父”项响应为“持久化”时,它才会保存推送的记录。如果您调用” class BlogPost < ActiveRecord::Base has_many :comments def add_comment(content) @new_comment = Comment.create(content) self.

因此,我的应用程序中有一些代码附加到一个与“有很多关系的文件中,当您使用
操作符(
收集)时,我知道,我在搜索“实际上,只有当“父”项响应为“持久化”时,它才会保存推送的记录。如果您调用”
class BlogPost < ActiveRecord::Base    
    has_many :comments

    def add_comment(content)
        @new_comment = Comment.create(content)
        self.comments << @new_comment
    end
end
self.comments << @new_comment
class Group < ActiveRecord::Base
  has_many   :users
  has_many   :avatars, through: :users
end

class User < ActiveRecord::Base
  belongs_to :group
  has_one    :avatar
end

class Avatar < ActiveRecord::Base
  belongs_to :user
end

@group.avatars << Avatar.new   # this would work if User belonged_to Avatar rather than the other way around