ruby - Rails: How to not create new join models for newly associated objects -
ruby - Rails: How to not create new join models for newly associated objects -
i'm working in ruby on rails 4.1.6. have 2 associated models (post , user) through 1 (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 new bring together model comment empty content. there way switch off automatic creation?
edit:
i'm populating database sample_data.rake file this:
. . . users = neighborhood.users.limit(6) category = postcategory.find(1) 50.times 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 when i'm creating new post user, comment created don't want.
edit 2:
in database looks 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
you have has_many :users, through: :comments
association here can't create post
associated user
without creating bring together model - comment
. consequence of info model.
ruby-on-rails ruby model has-many-through associated-object
Comments
Post a Comment