f



help on find over find

hi all

I have a problem trying to do the following:

MyModel.find(:all).find(:first)

it can look weird or unnecesary but this is the short explanation,
extended one is:

I have a helper method, this receives a collection an return some html,
this helper works fine if that collection is an association (has_many)
of a model but doesn't work if it is a direct find over a model

the error I receive is "LocalJumpError: no block given"

ultra mega extended explanation is:

a model:

class User < ActiveRecord::Base
 has_many :requests
end

other model:

class Request < ActiveRecord::Base
 def unRequested
  find(:all, conditions => {:state => "unrequested"})
 end
end

the helper
module ApplicationHelper
 def yearsCalendar (collection)
  years = collection.find(:all, :select => "YEAR(date_created) AS
years")
  years.each do |y|
   html << "<div>#{y}</div>"
  end
 end
end

finally at view:
<%= yearsCalendar(User.requests) # WORKS FINE %>

<%= yearsCalendar(Request.unRequested) #WONT WORK , RETURN ERROR:
"LocalJumpError: no block given" %>

any help?, thanks in advance

-- 
Posted via http://www.ruby-forum.com/.

0
Mauricio
1/20/2011 11:05:10 PM
comp.lang.ruby 48886 articles. 0 followers. Post Follow

2 Replies
1025 Views

Similar Articles

[PageSpeed] 21

On Jan 20, 4:05=A0pm, Mauricio Alcayaga <gusan...@hotmail.com> wrote:
> hi all
>
> I have a problem trying to do the following:
>
> MyModel.find(:all).find(:first)
>
> it can look weird or unnecesary but this is the short explanation,
> extended one is:
>
> I have a helper method, this receives a collection an return some html,
> this helper works fine if that collection is an association (has_many)
> of a model but doesn't work if it is a direct find over a model
>
> the error I receive is "LocalJumpError: no block given"
>
> ultra mega extended explanation is:
>
> a model:
>
> class User < ActiveRecord::Base
> =A0has_many :requests
> end
>
> other model:
>
> class Request < ActiveRecord::Base
> =A0def unRequested
> =A0 find(:all, conditions =3D> {:state =3D> "unrequested"})
> =A0end
> end
>
> the helper
> module ApplicationHelper
> =A0def yearsCalendar (collection)
> =A0 years =3D collection.find(:all, :select =3D> "YEAR(date_created) AS
> years")
> =A0 years.each do |y|
> =A0 =A0html << "<div>#{y}</div>"
> =A0 end
> =A0end
> end
>
> finally at view:
> <%=3D yearsCalendar(User.requests) # WORKS FINE %>
>
> <%=3D yearsCalendar(Request.unRequested) #WONT WORK , RETURN ERROR:
> "LocalJumpError: no block given" %>
>
> any help?, thanks in advance
>
> --
> Posted viahttp://www.ruby-forum.com/.

The objects returned by find(:all, ...) and User.requests are
different, and have different APIs.  In particular, find(:all) returns
an array, which has a find method defined on it, but it's find method
is very different from the find on an ActiveRecord class.

You might consider changing your yearsCalendar method to look more
like this:

def yearsCalendar (collection)
  years =3D collection.map {|c| Time.parse(c.date_created).year}
  years.inject("") {|c, i| c << content_tag("div", i)}
end

This avoids the fact that the collections are different classes, and
instead uses what they have in common, ie that they both inherit the
Enumerable module.

Good luck,

e.
0
Eric
1/20/2011 11:18:48 PM
thank you very much Eric, I'll look forward this way

-- 
Posted via http://www.ruby-forum.com/.

0
Mauricio
1/21/2011 8:37:48 PM
Reply: