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

Chris Olsen 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.

:slight_smile: Reinhart
http://teapoci.blogspot.com

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])

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, http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M000530
.

best.
Mike

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.

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:

Chris Olsen 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

I do find on has_many :throughs all the time. I reckpn there's
something else interesting in your User model

Fred