Forum: Ruby on Rails Problems with before_validation

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Fc582698581884352e745d1d4c64699d?d=identicon&s=25 Joshua Muheim (josh)
on 2006-03-24 14:11
Hi all

My visitors can create party organisator profiles, and I want assign
them as the creators (the field creator_id in the organisators table
references a member in the members table).
To do that I tried it with before_validation:

  def before_validation
    creator = Member.find 1
  end

Sadly, this does not work. The validation

  validates_presence_of :creator_id

Still tells me, that creator needs a value.
What's wrong there? Somehow the attribute gets not filled with a
value... I tried ith with self.creator = Member.find 1, and this would
work, but surely is not clean, because creator is no static variable...

Thanks for help.
Josh
5b132632f47e8c7e2a83157cef4649c8?d=identicon&s=25 Agnieszka Figiel (agnessa)
on 2006-03-24 18:14
Joshua Muheim wrote:
> Hi all
>
> My visitors can create party organisator profiles, and I want assign
> them as the creators (the field creator_id in the organisators table
> references a member in the members table).
> To do that I tried it with before_validation:
>
>   def before_validation
>     creator = Member.find 1
>   end
>
> Sadly, this does not work. The validation
>
>   validates_presence_of :creator_id
>
> Still tells me, that creator needs a value.
> What's wrong there? Somehow the attribute gets not filled with a
> value... I tried ith with self.creator = Member.find 1, and this would
> work, but surely is not clean, because creator is no static variable...
>
> Thanks for help.
> Josh

hi,

maybe "@creator"? BTW could you point me to some info about using self,
I thought you could use it like "this" in java, it's simply pointing to
the current object?

--
Agnieszka
This topic is locked and can not be replied to.