OK - I don’t understand this - and it doesn’t seem to help to add self
to this.
I have a personnel.rb model.
There is a definition in this model…
def doh_anniversary
x = 1
while doh.to_date + ( 365 * x ) <= Time.now.to_date
x += 1
end
doh + ( 365.25 * x ).floor
end
where doh is an actual column (date of hire)
In my view code, this worked…
<%= @personnel.doh_anniversary %>
but now I am putting it into a loop…
<% for personnel in @personnel %>
<%= personnel.doh_anniversary %>
<% end %>
and that gets me an error - undefined method doh.anniversary
and defining it as
def self.doh_anniversary
x = 1
while self.doh + (365 * x ) <= Time.now.to_date
…
or
while doh + …
still returns the same undefined method error
Any hints on how to make happy method and less madness?
Craig
>
> In my view code, this worked...
> <%= @personnel.doh_anniversary %>
>
> but now I am putting it into a loop...
>
> <% for personnel in @personnel %>
> <%= personnel.doh_anniversary %>
> <% end %>
>
If this is in the same view, it is understandable.
If @personnel is a single Personnel instance, acquired with
Personnel.find(some_id), then the for loop won’t work. If @personnel
in the second example comes from something like Personnel.find(:all,
some_query), then try the following.
Try rendering the class name of the personnel variable in the loop:
<%= personnel.class.name %> and let us know what you get.
Cheers,
Max
On 9/13/06, Craig W. [email protected] wrote:
x = 1
def self.doh_anniversary
Craig
Adding “self” is definitely not the right thing - doh_anniversary uses
the
values from a particular
instance, not the Personnel class.
I’m concerned about the error message quoted above - if it that’s the
message, than your view
code has swapped . and _ - which would obviously be trouble… Make sure
you’re really saying
personnel.doh_anniversary and not personnel.doh.anniversary (which tries
to
call a method
named “anniversary” on the “doh” value).
And just to be pedantic, I don’t think the loop in doh_anniversary is
really
what you want
anyway. From the name of the function, it seems that you’re looking to
calculate the
next anniversary of a particular date. This should work pretty much the
same:
def doh_anniversary(doh)
d = Date.new(Date.today.year, doh.month, doh.mday)
d = Date.new(Date.today.year+1, doh.month, doh.mday) if d < Date.today
d
end
Two notes on the above new code:
(1) It dies rather painfully if doh is on a leap day (2/29) - you may
need
to rescue from
that exception if that’s important to you.
(2) It returns today’s date if the anniversary is today. Change the < to
a
<= if that’s not
what you want.
Hope this helps,
Matt J.
[email protected]
President/Technical Director, Acme Art Company (acmeartco.org)
On Wed, 2006-09-13 at 17:03 +1000, Max M. wrote:
yeah - I did have to use a class method.
I forgot that when you iterate, the only values that are available to
the view are those that already exist and thus it isn’t possible to get
the column name (method) since it doesn’t exist in the array.
Some work via a class method was indeed the answer - thanks - it was a
long day and I should have realized that much earlier.
Craig