Find by association table

Discussion in 'Ruby' started by Greg Ma, Feb 16, 2010.

  1. Greg Ma

    Greg Ma Guest

    Hi,
    I did a the method below that return all students who has the method
    parameter in their tags collection.
    I am sure this is pretty by coded, could someone help clean it up please
    :)


    #method
    def find_student_by_tag(tag)
    result = Tag.find_by_name:)first, :conditions => ["name LIKE ?",
    "%#{tag}%"])
    students = Student.find:)all)

    students.each do |student|
    students.delete(student) unless
    students.tags.find_by_name(result.name)
    end
    students
    end

    Greg
    --
    Posted via http://www.ruby-forum.com/.
     
    Greg Ma, Feb 16, 2010
    #1
    1. Advertising

  2. Greg Ma

    Greg Ma Guest

    Greg Ma wrote:
    > Hi,
    > I did a the method below that return all students who has the method
    > parameter in their tags collection.
    > I am sure this is pretty by coded, could someone help clean it up please
    > :)
    >


    I correct myself, but there is still probably something cleaner possible

    #method
    def find_student_by_tag(tag)
    students = Student.find:)all)

    students.each do |student|
    students.delete(student) unless
    students.tags.find_by_name(tag)
    end
    students
    end
    >
    > Greg


    --
    Posted via http://www.ruby-forum.com/.
     
    Greg Ma, Feb 16, 2010
    #2
    1. Advertising

  3. Greg Ma

    Josh Cheek Guest

    [Note: parts of this message were removed to make it a legal post.]

    On Mon, Feb 15, 2010 at 11:49 PM, Greg Ma <> wrote:

    > Greg Ma wrote:
    > > Hi,
    > > I did a the method below that return all students who has the method
    > > parameter in their tags collection.
    > > I am sure this is pretty by coded, could someone help clean it up please
    > > :)
    > >

    >
    > I correct myself, but there is still probably something cleaner possible
    >
    > #method
    > def find_student_by_tag(tag)
    > students = Student.find:)all)
    >
    > students.each do |student|
    > students.delete(student) unless
    > students.tags.find_by_name(tag)
    > end
    > students
    > end
    > >
    > > Greg

    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >

    Based on the similar topic to your previous post, which seemed to be a Rails
    application (if you recall, I suggested you would have more luck at the
    Rails mailing list http://groups.google.com/group/rubyonrails-talk ), I
    would suggest that you should have an association between students and tags.
    Probably a has_and_belongs_to_many association through a join table (
    http://guides.rubyonrails.org/association_basics.html#the-has-and-belongs-to-many-association).

    If this is the case, then you should be able to simply say:
    def find_students_by_tag(tag)
    tag.students
    end
     
    Josh Cheek, Feb 16, 2010
    #3
  4. Greg Ma

    Greg Ma Guest

    Josh Cheek wrote:
    > On Mon, Feb 15, 2010 at 11:49 PM, Greg Ma <>
    > wrote:
    >
    >> #method
    >> > Greg

    >>
    >> --
    >> Posted via http://www.ruby-forum.com/.
    >>
    >>

    > Based on the similar topic to your previous post, which seemed to be a
    > Rails
    > application (if you recall, I suggested you would have more luck at the
    > Rails mailing list http://groups.google.com/group/rubyonrails-talk ), I
    > would suggest that you should have an association between students and
    > tags.
    > Probably a has_and_belongs_to_many association through a join table (
    > http://guides.rubyonrails.org/association_basics.html#the-has-and-belongs-to-many-association).

    It is a he-has-and-belongs-to-many-association
    > If this is the case, then you should be able to simply say:
    > def find_students_by_tag(tag)
    > tag.students
    > end


    No i cannot do this because the parameter isnt a tag object but just a
    string.
    I guess i could try this
    def find_students_by_tag(tag)
    res = Tag.find_by_name(tag)
    res.students unless res
    end
    --
    Posted via http://www.ruby-forum.com/.
     
    Greg Ma, Feb 16, 2010
    #4
  5. Greg Ma

    Josh Cheek Guest

    [Note: parts of this message were removed to make it a legal post.]

    On Tue, Feb 16, 2010 at 12:17 AM, Greg Ma <> wrote:

    > No i cannot do this because the parameter isnt a tag object but just a
    > string.
    > I guess i could try this
    > def find_students_by_tag(tag)
    > res = Tag.find_by_name(tag)
    > res.students unless res
    > end
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >

    Have you looked at the acts_as_taggable plugin?
    http://juixe.com/techknow/index.php/2006/07/05/acts-as-taggable-plugin/

    That page has an example posts = Post.find_tagged_with('tag1')

    Which looks like you should be able to say

    Student.find_tagged_with(tag)

    Rather than the method you are currently trying to create
    Student.find_students_by_tag(tag)

    (actually, if you were wanting to call it like that, then it should be
    defined "def self.find_students_by_tag" because it should be a method on the
    Student class rather than on some particular student)
     
    Josh Cheek, Feb 16, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MK
    Replies:
    2
    Views:
    2,518
  2. Mike
    Replies:
    6
    Views:
    1,178
  3. Alex Rast
    Replies:
    2
    Views:
    1,085
    Alex Rast
    Nov 10, 2004
  4. sonic
    Replies:
    1
    Views:
    553
    John Saunders
    Jan 7, 2005
  5. Mike
    Replies:
    1
    Views:
    141
    Phrogz
    Mar 12, 2007
Loading...

Share This Page