0

I'm working in Ruby on Rails 4.1.6. I have two associated models (Post and User) through another one (Comment).

User model:

class User < ActiveRecord::Base

  has_many :comments, dependent: :destroy
  has_many :posts, through: :comments
end

Post model:

class Post < ActiveRecord::Base

  has_many :comments, dependent: :destroy, :autosave => false
  has_many :users, through: :comments
end

Comment model:

class Comment < ActiveRecord::Base

  belongs_to :post, counter_cache: :comments_count
  belongs_to :user
end

When I'm creating new Post then new join model Comment with empty content. Is there any way to switch off that automatic creation?

EDIT:

I'm populating my database with sample_data.rake file like this:

.
.
.
users = neighborhood.users.limit(6)
category = PostCategory.find(1)
50.times do
  title = Faker::Lorem.sentence(1)
  content = Faker::Lorem.sentence(10)
  users.each { |user| user.posts.create!(title: title, content: content, neighborhood: neighborhood, user: user, post_category: category) }
end

And then when I'm creating new Post for User, comment is also created what I don't want.

EDIT 2:

In database it looks like this:

id  | user_id | post_id | content |         created_at         |         updated_at         
-----+---------+---------+---------+----------------------------+----------------------------
  1 |     100 |       1 |         | 2014-10-30 15:36:52.141408 | 2014-10-30 15:36:52.141408
  2 |      99 |       2 |         | 2014-10-30 15:36:52.173397 | 2014-10-30 15:36:52.173397
.
.
.
297 |      98 |     297 |         | 2014-10-30 15:37:00.184889 | 2014-10-30 15:37:00.184889
298 |      97 |     298 |         | 2014-10-30 15:37:00.215618 | 2014-10-30 15:37:00.215618
299 |      96 |     299 |         | 2014-10-30 15:37:00.237478 | 2014-10-30 15:37:00.237478
300 |      95 |     300 |         | 2014-10-30 15:37:00.258608 | 2014-10-30 15:37:00.258608
Damian
  • 3
  • 1
  • 3
  • I don't quite understand what you are asking. Are you asking whether a user can create a post without there being a comment automatically created? – nikkon226 Oct 30 '14 at 15:28
  • 1
    Show how you try to create your `Post` record? – Marek Lipka Oct 30 '14 at 15:29
  • I added it to my question and explained little more. – Damian Oct 30 '14 at 15:43
  • @Damian show your `Comment` with empty content. – Marek Lipka Oct 30 '14 at 15:53
  • Your data model seems flawed. The way it is, a post belongs to a user if he has commented on it once. Therefore, you don't get "user's own posts" but rather "posts that a user is discussing". Therefore, if you create a new such post for the user, it implies that he has discussed it so there should exist a comment that points it out. User's own posts would imply a post having a`user_id` and a direct `belongs_to :user` without `:through`. What functionality are you trying to implement? – D-side Oct 30 '14 at 16:38
  • @D-side I'm trying to implement usual post system with comments and likes. Posts have user_id of user, which created it. I wanted also to implement comments, but with easy comments finding for posts and users. So finally should I just create Comment model with post_id and user_id fields in tradicional way? – Damian Nov 07 '14 at 14:22
  • Yep, that's right. A `Comment` belongs to a `Post` and to a `User` in different ways (relevance and authorship respectively), that is, associations for these should be independent. – D-side Nov 07 '14 at 14:27
  • Ok, I'll do it in normal way. Thanks ;) – Damian Nov 10 '14 at 14:28

1 Answers1

1

You have has_many :users, through: :comments association here so you can't create Post associated to User without creating join model - Comment. This is consequence of your data model.

Marek Lipka
  • 50,622
  • 7
  • 87
  • 91