[Rails] Using session data in model

Johan Allard johan at allard.nu
Mon Feb 28 12:29:55 GMT 2005


On 2005-02-28, at 20.36, Jean Helou wrote:

> If the gender is required for validation, it should probably be part
> of the model in a way or another.

Well, it's not really required for validation. It's just they way that 
the model is, that some things applies to men only and some things 
apply to women only. If I want to validate the men only part I need to 
know the gender of the member. If we're talking about different types 
of cancer for instant, it seems strange to add gender to the model.

> Thus,I would add a gender attribute to the model (not necessarily 
> persisted)
> write a getter and a setter for it
> It probably is persisted or can be calculated so in the initilisation
> I would retrieve or calculate it accordingly to set the object initial
> state.

Hmm, If I add:

	def gender
		@session["member"].gender
	end

to the model, I'm no where closer to the solution since the validation 
is also handled within the same model and the @session variable won't 
be set here either.

> Then the controller just has to set the new values including gender,
> and your validation uses the attribute value.

I must be slow. How do you mean. I've already tried to set @gender = 
@session["member"].gender before calling the save (and validation) and 
that didn't work.

Best regards

//johan

> jean
>
> On Mon, 28 Feb 2005 14:18:33 +1100, Johan Allard <johan at allard.nu> 
> wrote:
>> Hi,
>>
>> I want to use my some session data when I validate som data in the
>> model. The specific problem I have is that I present different forms
>> data based on gender, and then dependent of the gender, there's
>> different fields that needs validation. I'm wondering what's the
>> preffered way of doing this. The session data is not present in the
>> model, i.e:
>>
>> class Myclass < ActiveRecord::Base
>>         belongs_to :member
>>
>>         def validate
>>                 errors.add_on_empty %w{ required_field } if 
>> @session["member"].gender
>> == 'm'
>>         end
>> end
>>
>> does not work since the @session["member"] variable is not set. I've
>> tried to set:
>>
>> @member = @session["member"]
>>
>> in my controller before I call Myclass.save and that doesn't seem to
>> work either. So how should this be handled?
>>
>> Best regards
>>
>> //johan
>>
>>
>> _______________________________________________
>> Rails mailing list
>> Rails at lists.rubyonrails.org
>> http://lists.rubyonrails.org/mailman/listinfo/rails
>>
>>
>>
>>
> _______________________________________________
> Rails mailing list
> Rails at lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 186 bytes
Desc: This is a digitally signed message part
Url : http://one.textdrive.com/pipermail/rails/attachments/20050228/2d4b9c7c/PGP.bin


More information about the Rails mailing list