This project is archived and is in readonly mode.

#311 ✓resolved
Jeff Dean

Add query-attribute methods to associations

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

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.

Comments and changes to this ticket

  • Jeremy Kemper

    Jeremy Kemper June 3rd, 2008 @ 06:44 AM

    Ah, that's pretty nice..

  • Michael Koziarski

    Michael Koziarski June 4th, 2008 @ 12:04 AM

    don't we already have has_comments?

  • Jeff Dean

    Jeff Dean June 4th, 2008 @ 02:25 AM

    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
    
    
  • Michael Koziarski

    Michael Koziarski June 4th, 2008 @ 04:11 PM

    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 :)

  • Pratik

    Pratik June 4th, 2008 @ 07:28 PM

    • Title changed from “[PATCH] - Add query-attribute methods to associations” to “Add query-attribute methods to associations”
  • Pratik

    Pratik June 17th, 2008 @ 06:53 PM

    • 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.

  • CancelProfileIsBroken

    CancelProfileIsBroken August 3rd, 2009 @ 02:50 PM

    • Tag set to activerecord, bugmash, edge, enhancement, has_many, patch, tested
  • Jeff Dean

    Jeff Dean August 3rd, 2009 @ 04:43 PM

    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.

  • CancelProfileIsBroken

    CancelProfileIsBroken August 3rd, 2009 @ 07:02 PM

    • 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.

  • CancelProfileIsBroken

    CancelProfileIsBroken August 3rd, 2009 @ 07:02 PM

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

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Attachments

Pages