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.
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])
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
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,
.
best.
Mike
On Sun, Jun 15, 2008 at 8:26 PM, Chris O. <
[email protected]> 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/
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
On Jun 16, 3:11 am, Chris O. [email protected]
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
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