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
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.
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!
It looks kinda perlish, but not quite as arcane.
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
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 $