# Getting "no block given" on find method - rails 2.1

**URL:** <https://www.ruby-forum.com/t/getting-no-block-given-on-find-method-rails-2-1/140542>\
**Category:** Rails\
**Created:** [June 16, 2008, 1:51am UTC](https://www.ruby-forum.com/t/getting-no-block-given-on-find-method-rails-2-1/140542 "2008-06-16T01:51:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrisolsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c6cbf5/32.png) [@chrisolsen](https://www.ruby-forum.com/u/chrisolsen)\
**Post date:** [June 16, 2008, 1:51am UTC](https://www.ruby-forum.com/t/getting-no-block-given-on-find-method-rails-2-1/140542/1 "2008-06-16T01:51:42Z")

</div>

I am trying to do a simple find through the current\_user but get an  
exception. Either someone spiked my Starbucks coffee and I am screwing  
up all over the place or there is something wrong.

def index  
@league = current\_user.leagues.find(13)  
end

# Error

LocalJumpError in Admin/schedule todosController#index  
no block given

# If I pass a block to it it works fine, but it is just messy:

current\_user.leagues.find{ |l| l.id = 13}

Thanks for the help.

---

<div class="post-metadata">

**Author:** ![chrisolsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c6cbf5/32.png) [@chrisolsen](https://www.ruby-forum.com/u/chrisolsen)\
**Post date:** [June 16, 2008, 2:26am UTC](https://www.ruby-forum.com/t/getting-no-block-given-on-find-method-rails-2-1/140542/2 "2008-06-16T02:26:00Z")

</div>

I have done it this way for as long as I can remember (although it  
hasn’t been that long).

Here is a snippet from a previous project (2.0.1) that works just fine:  
@listing = @account.listings.find(params[:listing\_id])

---

<div class="post-metadata">

**Author:** ![chrisolsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c6cbf5/32.png) [@chrisolsen](https://www.ruby-forum.com/u/chrisolsen)\
**Post date:** [June 16, 2008, 1:58am UTC](https://www.ruby-forum.com/t/getting-no-block-given-on-find-method-rails-2-1/140542/3 "2008-06-16T01:58:02Z")

</div>

Chris O. wrote:

> def index  
> @league = current\_user.leagues.find(13)  
> end

you can not use .find(id) in array of hashing. the another way is using  
select{|x| x.id==13} or collect{|x| x.id==13}

leagues is array of hashing of indexing of current\_user from relational  
table. it is the same like you do :

@league = [“id”=\>“13”,“id”=\>“14”,“id”=\>“15”,“id”=\>“16”].find(13)

it is impossible.

🙂 Reinhart  
[http://teapoci.blogspot.com](http://teapoci.blogspot.com)

---

<div class="post-metadata">

**Author:** ![chrisolsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c6cbf5/32.png) [@chrisolsen](https://www.ruby-forum.com/u/chrisolsen)\
**Post date:** [June 16, 2008, 3:24am UTC](https://www.ruby-forum.com/t/getting-no-block-given-on-find-method-rails-2-1/140542/4 "2008-06-16T03:24:15Z")

</div>

> > it is impossible.

If the current\_user AR class has a one-to-many or many-to-many  
association named “leagues” defined then you certainly can use find on  
this association in rails,

> **[ActiveRecord::Associations::ClassMethods](https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M000530)**
>
> Associations are a set of macro-like class methods for tying objects together through foreign keys.

.

best.  
Mike

---

<div class="post-metadata">

**Author:** ![chrisolsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c6cbf5/32.png) [@chrisolsen](https://www.ruby-forum.com/u/chrisolsen)\
**Post date:** [June 16, 2008, 3:24am UTC](https://www.ruby-forum.com/t/getting-no-block-given-on-find-method-rails-2-1/140542/5 "2008-06-16T03:24:14Z")

</div>

On Sun, Jun 15, 2008 at 8:26 PM, Chris O. \<  
removed\_email\_address@domain.invalid\> wrote:

> I have done it this way for as long as I can remember (although it  
> hasn’t been that long).
> 
> Here is a snippet from a previous project (2.0.1) that works just fine:  
> @listing = @account.listings.find(params[:listing\_id])

What is current\_user.leagues?

I suspect that account.listings is an active record association, as in

class Account \< ActiveRecord::Base  
has\_many :listings  
end

but current\_user.leagues is some kind of Ruby collection, like an array.

## A has\_many association masquerades as a Ruby array for most uses, but it changes the find method (which Array gets from the Enumerable module) to to a sequel query instead of yielding to a block.

Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![chrisolsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c6cbf5/32.png) [@chrisolsen](https://www.ruby-forum.com/u/chrisolsen)\
**Post date:** [June 16, 2008, 3:42am UTC](https://www.ruby-forum.com/t/getting-no-block-given-on-find-method-rails-2-1/140542/6 "2008-06-16T03:42:38Z")

</div>

> What is current\_user.leagues?
> 
> I suspect that account.listings is an active record association, as in
> 
> class Account \< ActiveRecord::Base  
> has\_many :listings  
> end
> 
> but current\_user.leagues is some kind of Ruby collection, like an array.
> 
> A has\_many association masquerades as a Ruby array for most uses, but it  
> changes the find method (which Array gets from the Enumerable module) to  
> to  
> a sequel query instead of yielding to a block.

# This is what I have:

class User \< ActiveRecord::Base  
has\_many :league\_admins  
has\_many :leagues, :through =\> :league\_admins  
end

class League \< ActiveRecord::Base  
has\_many :league\_admins  
has\_many :users, :through =\> :league\_admins  
end

class LeagueAdmin \< ActiveRecord::Base  
belongs\_to :league  
belongs\_to :user  
end

---

<div class="post-metadata">

**Author:** ![chrisolsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c6cbf5/32.png) [@chrisolsen](https://www.ruby-forum.com/u/chrisolsen)\
**Post date:** [June 16, 2008, 10:04am UTC](https://www.ruby-forum.com/t/getting-no-block-given-on-find-method-rails-2-1/140542/7 "2008-06-16T10:04:12Z")

</div>

On Jun 16, 3:11 am, Chris O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I guess someone spiked my coffee :). I have probably just never tried  
> to perform a find in the way I am now. Usually when I have a relation  
> like this the most I would do is view the related items, but I guess  
> the has\_many :through only party behaves like a normal has\_many.

I do find on has\_many :throughs all the time. I reckpn there’s  
something else interesting in your User model

Fred

---

<div class="post-metadata">

**Author:** ![chrisolsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c6cbf5/32.png) [@chrisolsen](https://www.ruby-forum.com/u/chrisolsen)\
**Post date:** [June 16, 2008, 4:11am UTC](https://www.ruby-forum.com/t/getting-no-block-given-on-find-method-rails-2-1/140542/8 "2008-06-16T04:11:10Z")

</div>

Chris O. wrote:

> > What is current\_user.leagues?
> > 
> > I suspect that account.listings is an active record association, as in
> > 
> > class Account \< ActiveRecord::Base  
> > has\_many :listings  
> > end
> > 
> > but current\_user.leagues is some kind of Ruby collection, like an array.
> > 
> > A has\_many association masquerades as a Ruby array for most uses, but it  
> > changes the find method (which Array gets from the Enumerable module) to  
> > to  
> > a sequel query instead of yielding to a block.
> 
> # This is what I have:
> 
> class User \< ActiveRecord::Base  
> has\_many :league\_admins  
> has\_many :leagues, :through =\> :league\_admins  
> end
> 
> class League \< ActiveRecord::Base  
> has\_many :league\_admins  
> has\_many :users, :through =\> :league\_admins  
> end
> 
> class LeagueAdmin \< ActiveRecord::Base  
> belongs\_to :league  
> belongs\_to :user  
> end

I guess someone spiked my coffee :). I have probably just never tried  
to perform a find in the way I am now. Usually when I have a relation  
like this the most I would do is view the related items, but I guess  
the has\_many :through only party behaves like a normal has\_many.

Is there a rails way to make an association between these two classes,  
since that is something else that no longer exists, or does a person  
just do it manually?

ex  
@league = League.new(params[:league])  
League.transaction do  
@league.save!  
@league\_admin = LeagueAdmin.new(:league\_id =\> @league.id, :user\_id  
=\> current\_user.id)  
end
