call a function for :condition in association
I want to display sent messages by the user. Problem is drafts and sent message contents are stored in the same table, so I want to put a condition on the following association from user.rb
has_many :sent_messages, :class_name => "Message", :foreign_key => "user_id", :conditions => [#it has been sent!]
I thought of using a is_sent method from message.rb
def is_sent
current_user开发者_StackOverflow.drafts.find_by_message_id(:first, self.id).empty?
end
How can i call this method in the :condition
of my association?
Would it be preferable to use a column in my Message table specifying if the stored message has been sent or not?
Thanks!
I would add a boolean sent
column to Message
, and use this condition in the has_many
:
:conditions => { :sent => true }
This would also give you the function sent?
in Message
, eliminating your is_sent
function. Note that using a question mark like that in function names in Ruby is common practice, and is_
is frowned upon.
精彩评论