Reputation: 3090
Two models, Organization and Member, have a 1:many relationship. On basis of an earlier question, I added to my Organization model file:
validate :check_member
private
def check_member
if members.empty?
errors.add(:base, 'User is not present')
end
end
As a result it only creates a new organization if also a member for that organization is created. I adjusted my seeds file accordingly since I would now be unable to create an organization without a member:
Organization.create!( name: "Fictious business",
address: Faker::Address.street_address,
city: Faker::Address.city,
website: Faker::Internet.url,
subscription: true,
subs_exp_date: Faker::Date.forward(365),
actioncode: 111,
members_attributes: [email: "[email protected]",
username: "helpzzzz",
password: "foobar",
password_confirmation: "foobar"])
However it fails to seed the above, giving the error message: "ActiveRecord::RecordInvalid: Validation failed: User is not present" (referring to the error message defined in def check_member
).
Does anyone have an idea what I might be doing wrong?
UPDATE: It looks like it is something else that's creating the problem. In development I have no problem with seeding. However, seeding to Heroku is when the problem arises. By changing the seeds file, I have found out that when entering heroku run rake db:seed
it is not seeding with the current, updated seeds file (while it is doing so, in development). It is using the old seeds file where the second part members_attributes
was not included yet.
What could be the cause of this? A problem at Heroku's side? I've also tried heroku pg:reset DATABASE
, heroku run rake db:migrate
and then heroku run rake db:seed
but it was still using an old seeds file.
Upvotes: 0
Views: 469
Reputation: 15515
Create the members first, then the organization:
member = Member.create!(email: ...)
Organization.create!(name: ..., members: [member])
Regarding Heroku: Did you push your latest changes to Heroku?
Upvotes: 2