1

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

Akhil
  • 1,918
  • 5
  • 30
  • 74

1 Answers1

0

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.

Sunil D.
  • 17,983
  • 6
  • 53
  • 65
  • thanks. I worked out i got it. But a small change. I don't want record_status field value in the request. Instead i need to give as default. How i merge record_status value of associated table in the params. – Akhil Aug 02 '15 at 03:08
  • You can take it out of the permitted parameters then. Let the client post the value, and the then the server will ignore it b/c it not's a permitted param. Or delete the property before your client posts it to the server. – Sunil D. Aug 02 '15 at 17:12
  • I can restrict the parameters from the client. But i need to include that parameters inside my controller. I mean that parameters needs to be given internally not client. Here i need to remove that parameters from the permitted parameters and need to merge in the controller. But merge not worked please help – Akhil Aug 05 '15 at 01:19
  • I don't understand what you're doing. You should either edit this question and show thel code that is not working or post a new question. If this answer has been helpful, it doesn't hurt to up vote it. – Sunil D. Aug 05 '15 at 16:32
  • ok i closed this question and created another for my issue to avoid confusions. – Akhil Aug 06 '15 at 01:07
  • Please see the issue from the link http://stackoverflow.com/questions/31845402/merge-nested-attributes – Akhil Aug 06 '15 at 01:32