开发者

When creating a record off nesting, why isn't the parent id being added to the child?

Given the following

Thread
   belongs_to :user

User
   has_many :threads, :through => :thread_members

When I do:

  @user.threads.create(:title => "hello")

In my Thread model I have a before_create, and in that method I and Rails.logger.info self.inspect which outputs:

#<Thread id: nil, user_id: nil, title: "hello" ...

Why is user_id not being assigned? Than开发者_如何学运维ks


Check that the @user is already saved, the parent records must be saved before the children get their foreign keys set. Please show the controller code that loads that @user object.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜