has_many conditions or proc on foreign key

Posted by ere on Stack Overflow See other posts from Stack Overflow or by ere
Published on 2013-07-02T22:25:01Z Indexed on 2013/07/02 23:05 UTC
Read the original article Hit count: 190

I have a has_many association between two models using a date as both the foreign and primary key for each model.

It works perfectly one way but not the other.

Works

has_one :quiz_log, :primary_key => :start_at, :foreign_key => :start_at

Doesn't work

has_many :event_logs, :primary_key => :start_at, :foreign_key => :start_at 

The reason being (i think) because the start_at on QuizLog is a date and the start_at on EventLog is a datetime. So it returns nil trying to match the exact datetime on a simple date.

How can I cast the foreign_key start_at on the second statement to convert it first from datetime to simply date so it will match the second model?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby-on-rails-3