Why do you say HABTM is deprecated? Just curious. I was always under the
impression you use HABTM if you just want a simple relationship with no
extra information and the :through option if you needed a more complex
setup.
Why do you say HABTM is deprecated? Just curious. I was always under the
impression you use HABTM if you just want a simple relationship with no
extra information and the :through option if you needed a more complex
setup.
I read this in trac:
DEPRECATED: Using additional attributes on has_and_belongs_to_many
associations. Instead upgrade your association to be a real join model
[DHH]
p.children[0].position
=> NoMethodError: undefined method position' for #<Child:0xb76b4114> from /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/attribute_methods.rb:205:inmethod_missing’
from (irb):3
And p.children[0].position is what I would like to use. When I use
has_and_belongs_to_many this works.
Which is right, Child has not position attribute it’s in the
ChildrenParent.
Now the problem is that a child has_many children_parents, so we can’t
just do
p.children[0].children_parent.position
We need to say WHICH of the many possible childrenParents we’re
talking about, Now I’m guessing that the logic actually dictates that
there really is only one, in which case we can do
However the right way to do this, IMHO would be to do this:
class Child
def position
children_parent.find(:first).position
end
end
But of course this raises the question of why position isn’t just a
simple attribute of Child, in which case I’d seriously consider
dropping back to HABTM.
If there really are multiple ChildrenParents associated with a single
Child, then you do need to figure out which one you are getting the
position of.
I’m creating a website where people (at our company) can vote at songs.
The song with the most votes gets played.
So I now have:
Playlist, Song, PlaylistItem
But I dont’t find:
p = Playlist.find(:first)
p.playlist_items[0].position
p.playlist_items[0].top_top
p.songs[o].title
p.songs[o].artist
make sence.
Maybe it’s the naming of the objects I’ve done. Does someone know a
better name for the join class so it makes more sence doing the
position-thing on that model instead of song?
Maybe it’s the naming of the objects I’ve done. Does someone know a
better name for the join class so it makes more sence doing the
position-thing on that model instead of song?
So where are the votes in this model?
Why not put a vote count attribute in the Song which voting increments?
If you want to actually hold the votes in the database then you can do
something like:
class Vote < ActiveRecord::Base
belongs_to :song
belongs_to :voter
…
end
def Song < ActiveRecord::Base
has_many :votes, :counter_cache => true # and add a votes_count
column to the table
def self.most_popular
find(:first, :order => “vote_count DESC”)
end
…
end