![](/img/trans.png)
[英]Ruby on Rails - delegate devise member on has_and_belongs_to_many association
[英]Ruby on Rails has_and_belongs_to_many association issues
所以基本上我有兩個模型:
class User < ActiveRecord::Base
has_and_belongs_to_many :post
end
class Post < ActiveRecord::Base
has_and_belongs_to_many :user
end
我的遷移是:
rails g migration CreateJoinTablePostsUsers posts users
但是如果在我的控制台中輸入:
@post = Post.where(:id => User.find_by_email("me@mail.com").post_ids).last
但我得到零
我在終端檢查了一下:
Post.last.user_ids
和:
User.last.post_ids
他們兩個都返回了空
在此之前,已經回答了這個問題: 將記錄添加到has_and_belongs_to_many關系中 。
基本上,您需要互相分配值。
post = Post.find(x)
user = User.last
user.posts << post
它們返回空值,因為它們為空,需要分配其值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.