Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Add query-attribute methods to associations

#311

This patch adds query-attribute methods to associations.


class Post < ActiveRecord::Base
 has_one :author
 has_many :comments
 has_many :readers, :through => :comments
 belongs_to :blog
 has_one :latest_comment, :through => :comments
end

post = Post.new
post.author?
post.comments?
post.readers?
post.blog?
post.latest_comment?

This patch includes tests and documentation and takes into account the differences with has_one :through.

I added these methods so that associations behave like normal attributes and provide a way to provide a guaranteed true/false without having to know about the underlying implementation.

Reported by Jeff Dean · June 3rd, 2008 @ 05:42 AM

State: resolved
Milestone: none
Assigned to: Pratik Pratik
Importance: none

Activity

  1. Jeremy Kemper
    Jeremy Kemper

    Ah, that's pretty nice..

    June 3rd, 2008 @ 06:44 AM

  2. Michael Koziarski
    Michael Koziarski

    don't we already have has_comments?

    June 4th, 2008 @ 12:04 AM

  3. Jeff Dean
    Jeff Dean

    I don't see a has_tasks? method in irb or in the docs.

    
    class Project < ActiveRecord::Base
      has_many :tasks
    end
    
    class Task < ActiveRecord::Base
      belongs_to :project
    end
    
    >> puts Project.new.methods.sort.select{|m| m=~ /task/}
    after_create_or_update_associated_records_for_tasks
    before_save_associated_records_for_tasks
    task
    task_ids
    task_ids=
    tasks
    tasks=
    validate_associated_records_for_tasks
    
    

    June 4th, 2008 @ 02:25 AM

  4. Michael Koziarski
    Michael Koziarski

    Oh right, we deprecated it and removed it for 2.0 in favour of people.empty? etc.

    If jeremy's down with the addition then I'm for it too :)

    June 4th, 2008 @ 04:11 PM

  5. Pratik
    Pratik
    • Title changed from [PATCH] - Add query-attribute methods to associations to Add query-attribute methods to associations

    June 4th, 2008 @ 07:28 PM

  6. Pratik
    Pratik
    • Assigned user set to Pratik
    • State changed from new to incomplete
    • Milestone cleared.

    Ok Jeremy likes it. So lets add it :-)

    Jeff, could you please generate a new patch git-format-patch ? You can find details at http://rails.lighthouseapp.com/p...

    Also, could you add tests to check the association doesn't get loaded when someone calls association? method.

    Thanks.

    June 17th, 2008 @ 06:53 PM

  7. CancelProfileIsBroken
    CancelProfileIsBroken
    • Tag set to activerecord, bugmash, edge, enhancement, has_many, patch, tested

    August 3rd, 2009 @ 02:50 PM

  8. Jeff Dean
    Jeff Dean

    I forgot this patch was still out there!

    Since we now have .present?, this patch could be simplified to just adding an alias. However, since we have .present?, and it's hip and super readable, we could also just delete this ticket.

    August 3rd, 2009 @ 04:43 PM

  9. CancelProfileIsBroken
    CancelProfileIsBroken
    • Tag changed from activerecord, bugmash, edge, enhancement, has_many, patch, tested to activerecord, edge, enhancement, has_many, patch, tested
    • State changed from incomplete to resolved

    Closing, since we now have a better alternative. Thanks for the update Jeff.

    August 3rd, 2009 @ 07:02 PM

  10. CancelProfileIsBroken
    CancelProfileIsBroken

    Closing, since we now have a better alternative. Thanks for the update Jeff.

    August 3rd, 2009 @ 07:02 PM