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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With