I was wondering if anybody knows an easy way to pass a method name as
the scope option to a validates_uniqueness_of validation on a AR model?
For example, I have nested models a bit like:
class Survey
has_many :areas
end
class Area
belongs_to :survey
has_many :questions, :order => ''position''
validates_uniqueness_of :position, :scope => : survey_id
end
class Question
belongs_to :area
validates_uniqueness_of :position, :scope => : area_id
end
So to get the questions on a survey, sorted by the
''survey_position''
attribute I have to add a foreign key to the question, so that I can
pass the survey_id to the :scope argument on validates_uniquess_of. So,
I have to add this stuff to Survey and Question classes:
class Survey
has_many :questions, :order =>
''questions.survey_position''
end
class Question
belongs_to :survey
validates_uniqueness_of :survey_position, :scope => :survey_id
end
This creates problems for when saves cascade down associated objects.
Any ideas on how to pass a method name as the scope option? Then I could
add (instead of the above):
class Survey
def questions
find_all_questions.sort_by(:survey_position)
end
end
class Question
validates_uniqueness_of :survey_position, :scope =>
:survey_id_from_area
def survey_id_from_area
area.survey.id
end
end
Any ideas?
~ Mark
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---