#311 √ incomplete
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

    • → Milestone cleared.
    • → State changed from “new” to “incomplete”
    • → Assigned user changed from “” to “Pratik”

    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.

Please Login or create a free account to add a new comment.

You can update this ticket by sending an email to from your email client. (help)

Create your profile

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

Source available from github

The Git repository resides at http://github.com/rails

Check out the current development trunk (Edge Rails) with:

git clone git://github.com/rails/rails.git

Creating or reviewing a patch

See the contributor guide.

Creating a feature request

Please don't. If you want a new feature in Rails, you'll have to pull up your sleeves and get busy yourself. Or convince someone else to do it. See the contributor guide on how to get going. But posting them here is just going to lead to ticket root.

Creating a bug report

When creating a bug report, be sure to include as much relevant information as possible. Post the code sample that causes the problem. Preferably, alter the unit tests and show through either changed or added tests how the expected behavior is not occuring.

Security vulnerabilities should be reported via an email to security@rubyonrails.org, do not use trac for reporting security vulnerabilities. All content in trac is publicly available as soon as it is posted.

Then don't get your hopes up. Unless you have a "Code Red, Mission Critical, The World is Coming to an End" kinda bug, you're creating this ticket in the hope that others with the same problem will be able to collaborate with you on solving it. Do not expect that the ticket automatically will see any activity or that others will jump to fix it. Creating a ticket like this is mostly to help yourself start on the path of fixing the problem and for others to sign on to with a "I'm having this problem too".

Shared Ticket Bins