-1

How can i accomplish this?

class User < ActiveRecord::Base     
  has_many :old_posts
  has_many :new_posts
end

I already have has_many relation to old_posts model. I would like to add another has_many relation to new_posts. The old_posts and new_posts are no way related. I'm in the process of refactoring my schema, I would like to support the 'old_posts' model for few months before I get rid of it totally.

I get this error in schema when I try to migrate

#Could not dump table "Users" because of 
following NoMethodError
#undefined method `[]' for nil:NilClass
Lollypop
  • 251
  • 1
  • 5
  • 14

1 Answers1

1
  • You should specify conditions as both are referring to same model.

  • Mention model it's referring to explicitly as it won't identify model by default because of custom association names.

Update code like below

class User < ActiveRecord::Base
    has_many :old_posts, ->{where("created_at > ?", Time.now.end_of_day)}, class_name: 'Post', foreign_key: 'user_id'
    has_many :new_posts, ->{where("created_at < ?", Time.now.end_of_day)}, class_name: 'Post',foreign_key: 'user_id'
end
krishnar
  • 2,537
  • 9
  • 23