This is helping…getting further.
I tried this:
group.topic.find(:first, :conditions=>[ “symbol=?”, “LM”])
and get:
ActiveRecord::RecordNotFound: Couldn’t find Topic with ID in (‘—
:first’,'— \n:conditions: \n - "symbol=?"\n - L
M’)
from
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/associa
tions/has_and_belongs_to_many_a
ssociation.rb:77:in `find’
from (irb):112
from :0
“symbol” is a column in the “Topics” table
Not sure why though???
From: [email protected]
[mailto:[email protected]] On Behalf Of Chris H.
Sent: Wednesday, November 30, 2005 8:02 AM
To: [email protected]
Subject: Re: [Rails] active record question
i suppose you could even do
group = Group.find(1)
topic = group.topics.find(1234)
documentation doesn’t specify what is returned from find so it could be
a
RecordNotFound exception or nil if the record does not exist.
On 11/30/05, Chris H. <[email protected] > wrote:
ah, i misread…
given an arbitrary topic, you want to see if its associated with a
group…
easy.
arbitrary_topic = Topic.find(1234)
group = Group.find(1, :include => :topics)
find out if arbitrary topic is associated with group
if group.topics.include?(arbitrary_topic)
found the topic in the group
else
did not find topic in group
end
On 11/29/05, Phil S. < [email protected]
mailto:[email protected] > wrote:
I have a many to many relationship for two tables (topics and groups)
and a
join table for the two tables (groups_topics)
I have code working where I can access all the topics for a group by
group.topic, which returns an array of topic instances. This works
fine.
The problem is that there might be 1000s of topics valid for a
particular
group. One use case I have is I just want to see if one specific topic
is
valid for a group. Using the above, I’d have to iterate the array to
find
this out. It seems like there should be a faster way where I can
leverage a
condition (“topic_name=blah”) instead of returning/iterating a huge
result
set.
I saw a blurb in the agile rails book on this subject, but it didn’t
give
enough detail for me to understand…
Can someone point me in the right direction?
Thanks,
phil