Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Association "is invalid" when model is saved

I have a Club model and a Member model, which are associated through a Membership model. In other words

class Club < ActiveRecord::Base
  has_many :members, :through => :memberships
end

class Member < ActiveRecord::Base
  has_many :clubs, :through => :memberships
end

However, when I attempt to create a new member and add it to a club, I get an error saying that the club is invalid.

> club = Club.find(1)
> member = Member.new(:name => 'Member Name')
> member.clubs << club
> member.save

The member.save statement will return false. Looking at the member.errors.messages, I find

> member.errors.messages
@messages={:clubs=>["is invalid"]}

The really bizarre thing is that this does not happen for my Development environment (using sqlite3), but only on my EngineYard deployment using mySQL.

like image 364
Dave Isaacs Avatar asked Mar 16 '13 21:03

Dave Isaacs


1 Answers

I just figured out my own problem. My Club class contains a virtual attribute :password, which is used only when a club is created, and should be ignored otherwise. It turns out that due to a bug, the password is not being ignored otherwise and is being validated when the club association is saved. So Jim Stewart was correct in his comment: the club is actually NOT valid, even though I thought it was.

The reason the problem does not occur in development is that I have password validation turned off in my development environment, so I can test with simple passwords.

like image 116
Dave Isaacs Avatar answered Sep 29 '22 09:09

Dave Isaacs