如何使用 HABTM 关联将 user_id 包含到呈现的 json
How to include user_id to rendered json with HABTM associations
我有以下型号的控制器:
article.rb
class Article < ActiveRecord::Base
has_and_belongs_to_many :users
end
user.rb
class User < ActiveRecord::Base
has_and_belongs_to_many :articles
end
articles_controller.rb
def show
@article = Article.find(params[:id])
render json: @article
end
如何将 user_ids
包含在 renered json 中?
我试过:
render json: Article.find(params[:id]), include: :user_ids
得到了
NoMethodError - undefined method `serializable_hash' for 1:Fixnum:
你可以试试
render json: @article.to_json(include: [:users])
我有以下型号的控制器:
article.rb
class Article < ActiveRecord::Base
has_and_belongs_to_many :users
end
user.rb
class User < ActiveRecord::Base
has_and_belongs_to_many :articles
end
articles_controller.rb
def show
@article = Article.find(params[:id])
render json: @article
end
如何将 user_ids
包含在 renered json 中?
我试过:
render json: Article.find(params[:id]), include: :user_ids
得到了
NoMethodError - undefined method `serializable_hash' for 1:Fixnum:
你可以试试
render json: @article.to_json(include: [:users])