Forum: Ruby on Rails Strange error using association methods

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
970efdae7d4dd77370743065457b17fe?d=identicon&s=25 steven.mohapibanks (Guest)
on 2005-11-17 16:20
(Received via mailing list)
Hi,

I've recently replaced a rather clunky old piece of code:

user = User.find(id)
meeting_attendees = user.meetings.map { |m| m.id }
meetings = Meeting.find(:all, :include =>
[:users, :groups], :conditions => ['meetings.date_and_time > ? and
meetings.id in(?)', Time.now, meetings_attendees])


with

user.meetings.find(:all, :conditions => ['meetings.date_and_time
 > ?', Time.now])

I'm having a strange error whereby an attribute called "status" in
the resulting list of meetings is being set to '0', but not for every
meeting record. Just randomly (although it's the same records each
time). Now the problem appears to be that because the join table also
has a "status" attribute (the status of each individual attendee
rather than the actual event status) that the call is getting
confused in some way.

Will I need to write my own association extension to resolve this (or
indeed just rewind to the original code)?

Kind regards,
Steven
2ccb2f7699267d258faaafce4ee997df?d=identicon&s=25 codyfauser (Guest)
on 2005-11-17 17:54
(Received via mailing list)
Steven,

If the user has_and_belongs_to_many :meetings then the status
attribute will be automatically injected into the objects returned
because you're accessing the objects through the association.

The value of status is being read from the model's table and then
clobbered by the value from the join table.  You could rename the
status columns so that they don't collide.

See:
http://api.rubyonrails.com/classes/ActiveRecord/As...
for more details.

You also don't need the 'meetings' in the conditions:

user.meetings.find_all_for_date_and_time(:all, :conditions =>
['date_and_time
 > ?', Time.now])

Hope this helps.


Cody
This topic is locked and can not be replied to.