ruby on rails - accepts_nested_attributes_for :reject_if to trigger another method -
i've got multi-level nested form using formtastic_cocoon (jquery version of formtastic).
i trying validation in sense of
if value is_numeric insert database else database lookup on text insert id association end
i hoping tha accepts_nested_attributes_for have :if option, apparently there :reject_if.
is there way create validation describe part of accepts_nested_attributes_for??
-----------------------updated per zubin's response ---------------------------
i believe zubin on right track method, can't seem working right. method using is
def lookup_prereq=(lookup_prereq) return if lookup_prereq.blank? case lookup_prereq when lookup_prereq.is_a?(numeric) == true self.task_id = lookup_prereq else self.task = task.find_by_title(lookup_prereq) end end
when trigger function, self.task_id being put in database '0' rather task.id.
i'm wondering if i'm missing else. i'm not sure method being called. shouldn't need
lookup_prereq(attr[:prereq_id)
at point?
-------------------further edit ----------------------- think can find method called if named same name value database, therefore i've changed method
def completed_task=(completed_task)
unfortunately still resulting in 0 value in database.
sounds need method in nested model handle that, eg:
class post < activerecord::base has_many :comments accepts_nested_attributes_for :comments end class comment < activerecord::base belongs_to :post belongs_to :author def lookup_author=(lookup_author) return if lookup_author.blank? case lookup_author when /^\d+$/ self.author_id = lookup_author else self.author = author.find_by_name(lookup_author) end end end
Comments
Post a Comment