Please someone help with this active record question! its driving me crazy

On Dec 27 2007, 10:36 am, “Jeremy McAnally” [email protected]
wrote:

Oh, okay. You could do this then (I think):

@avatars = @user.admireds.map{|adm| Avatar.find_by_user_id(adm.id)}
[…]

Would you explain this a bit?

@avatars is an object.

Is it a collection of users?

thanks,

Thufir

On Dec 26 2007, 10:17 am, “Rick DeNatale” [email protected]
wrote:
[…]

will automatically be populated.
[…]

Am I alone in wondering what replaces push_with_attributes? This is
in regards to has_many through.

thanks,

Thufir

okay so,
##################################
class User < ActiveRecord::Base
has_many :admirations, :foreign_key => ‘admirer_id’, :class_name =>
‘Admiration’
has_many :admirees, :through => :admirations

has_many :admireds, :foreign_key => ‘admiree_id’, :class_name =>
‘Admiration’
has_many :admirers, :through => :admireds

has_one :avatar
end

=======================================================================

class Admiration < ActiveRecord::Base
belongs_to :admirer, :class_name => “User”
belongs_to :admiree, :class_name => “User”
end

========================================================================

class Avatar < ActiveRecord::Base
belongs_to :user
end

##############################################
Is this right so far?

user = User.find(1)
user.admirers #=> Array of User Objects
user.admirees #=> Array of User Objects
user.admirers.map{|a| a.avatar} #=> Array of Avatar Objects
user.avatar #=> Avatar Object

anything I’ve missed?

No, @user.admireds is a collection of User objects.

When you do #map, it iterates the collection and replaces the current
index with the return value of the block. So in this case, an array
of User objects is transformed into an array of avatars for those
users. So, @avatars becomes an array of Avatar objects.

Read up on the #map method and it will make more sense. :slight_smile:

–Jeremy

On Jan 18, 2008 2:56 AM, Thufir [email protected] wrote:

Would you explain this a bit?


http://www.jeremymcanally.com/

My books:
Ruby in Practice

My free Ruby e-book

My blogs:

http://www.rubyinpractice.com/

has_many :through replaced it. If you need attributes on yr join model,
has_many :through is the way to do it. has_many :through doesn’t need
push_with_attributes because it has an ActiveRecord model with proper
attributes. You can just create an instance of that model and set its
attributes as needed.

On Fri, 18 Jan 2008 11:04:17 -0500, Jeremy McAnally wrote:

When you do #map, it iterates the collection and replaces the current
index with the return value of the block. So in this case, an array of
User objects is transformed into an array of avatars for those users.
So, @avatars becomes an array of Avatar objects.

Read up on the #map method and it will make more sense.

I’m going have to RTFM! :slight_smile:
It looks kinda perlish, but not quite as arcane.

-Thufir

On Fri, 18 Jan 2008 10:25:36 -0500, Russell N. wrote:

has_many :through replaced it. If you need attributes on yr join model,
has_many :through is the way to do it. has_many :through doesn’t need
push_with_attributes because it has an ActiveRecord model with proper
attributes. You can just create an instance of that model and set its
attributes as needed.

Yes, I’m realizing this :slight_smile:

How do I go about creating an instance of the model, please?

thufir@arrakis ~/strawr $
thufir@arrakis ~/strawr $ cat -n app/controllers/feeds_controller.rb |
tail -n 47 | head -n 14
68
69
70 def add_tag
71
72 @feed = Feed.find(params[:id])
73 @feed.tags.push_with_attributes(
74 Tag.find(params[:tag][:id]))
75
76 if @feed.save
77 flash[:notice] = ‘Tag has been added!’
78 end
79 redirect_to :action => ‘show’, :id => params[:id]
80 end
81
thufir@arrakis ~/strawr $

above code is at:
http://strawr.googlecode.com/svn/trunk/

thanks,

Thufir

On Jan 18, 10:20 am, Thufir [email protected] wrote:

Read up on the #map method and it will make more sense.

I’m going have to RTFM! :slight_smile:
It looks kinda perlish, but not quite as arcane.

It’s actually kinda Smalltalkish, if that makes it any better. :slight_smile:

///ark