I didn't get any responses and decided to that I should tackle this
validation another way. However I did play with adding attributes to
my classes, which is probably elementary to most here but it impressed
me and perhaps some novice Ruby folks such as myself may benefit?
class Test
def hello
'hello'
end
def self.add_acc name
attr_accessor name
end
end
words = ['nick', 'last_name', 'fav_bike']
for w in words
Test.add_acc w
end
t = Test.new
puts t.hello
t.send words[0] + '=', 'mj'
puts t.send( words[0])
t.send words[1] + '=', 'jones'
puts t.send(words[1])
t.send words[2] + '=', 'surly'
puts t.send(words[2])
Cheers-
MJ
On 1/17/06, Michael Jones <[EMAIL PROTECTED]> wrote:
> Hello-
>
> I've been reading and hacking and reading and hacking and getting
> frustrated cause my Java background never touched this metaprogramming
> stuff. I was hoping someone out there could help.
>
> Basically I want to use the ActiveRecordValidation on a a tableless
> *dynamic* model. I've found lots of good stuff on validating tabless
> models, it's the dynamic part that alludes me.
>
> I was hacking around with some code I found here:
> http://rails.techno-weenie.net/tip/2005/11/19/validate_your_forms_with_a_table_less_model
> I posted the code from that link below.
>
> The dynamic part is: I have questions in the database, these questions
> have 'name', 'validation', 'errormessage' and some other fields. I
> display these questions on a page , when they submit, I need to take
> this collection of questions (with the users answers), and validate
> them. Therefore my generic-tableless-model needs to take this
> collection, create instance attributes for each question.name, then
> call the given validation (question.validation) on the given value
> (question.answer) and return question.errormessage if the validation
> fails.
> (validation and errormessage are comma separated so that multiple
> validations can be run)
>
> Any help/pointers/suggestions are very much appreciated!
>
> Thanks-
> Michael
>
> 1 class Contact < ActiveRecord::Base
> 2 def self.columns() @columns ||= []; end
> 3 def self.column(name, sql_type = nil, default = nil, null = true)
> 4 columns <<
> ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default,
> sql_type.to_s, null)
> 5 end
> 6
> 7 column :name, :string
> 8 column :city, :string
> 9 column :state, :string
> 10 column :phone, :string
> 11 column :email_address, :string
> 12
> 13 validates_presence_of :name, :email_address
> 14 end
>
_______________________________________________
PDXRuby mailing list
[email protected]
IRC: #pdx.rb on irc.freenode.net
http://lists.pdxruby.org/mailman/listinfo/pdxruby