Nested routes and CRUD operations with additional values for assciation in Rails

偶尔善良 提交于 2019-12-13 01:19:34

问题


I created one has_many through relationship in rails api. I also used nested routes.

My models like below;

class Author < ActiveRecord::Base
 has_many :comments
 has_many :posts, :through => :comments
end

class Post < ActiveRecord::Base
 has_many :comments
 has_many :authors, :through => :comments
end

class Comment < ActiveRecord::Base
 belongs_to :author
 belongs_to :post
end

my routes like below;

Rails.application.routes.draw do
 namespace :api, defaults: { :format => 'json' } do
  resources :posts do
   resources :comments
  end
  resources :authors
 end
end

So here my aims are

  1. Comments are nested route so that i can create and display comments from post
  2. Here not any post author. The author is meant for comment owner

I implemented the concepts and working almost all. But i am facing the following 2 problems with associations

  1. How to add additional fields for associated table when parent create. Here my requirement is when a post is created, i need to insert one default entry for comment. My post controller implementation for create is like below
def create
  params = create_params.merge(:record_status => 1)
  @post = Post.new(params)
  @post.authors << Author.find(1)
  if @post.save 
   render :show, status: :created
  end
 end

 def show
  @post = Post.find(params[:id])
 end

private
def create_params
 params.permit(:name, :description, :author_id )
end

Here i am passing author_id in the request json. This needs to be added as author id in the comments table. Now i just hard coded as 1. I used '<<' for association entry. This is working but i also need to include two more fields which are :comments and :record_status. Again :comments is from the request itself.

Note: This is not rails mvc application. This is rails api and input as json.

  1. When i display comments using nested routes i need to show author and also comments from comments table. My comments controller method is ;
class Api::CommentsController < ApplicationController
 before_filter :fetch_post

 def index
    @authors = @post.authors.where(:record_status => 1, comments: { record_status: 1 })
end

private

def fetch_post
 @post = Post.find(params[:post_id])
end

end

Here i got authors but not correct comments in the join table 'comments'

Please help me to solve these issues


回答1:


For the first problem, you want to configure the posts_controller to accept the nested attributes for comments. Add this line at the beginning of your controller:

accepts_nested_attributes_for :comments

Check the documentation for further details on this method.

Then, you need to modify the parameters that the controller will allow:

def create_params
 params.permit(:name, :description, :author_id, comments_attributes: [ :author_id, :comments, :record_status ] )
end

Modify the attributes listed in the comments_attributes array to match those of your Comment model.

I'm not sure what you're trying to get in the second problem, but maybe you just need to query a little differently:

@comments = Comment.where(post_id: @post.id).includes(:author)

The above would return a list of comments including the comment author.



来源:https://stackoverflow.com/questions/31763795/nested-routes-and-crud-operations-with-additional-values-for-assciation-in-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!