Skip to content

markgandolfo/associated_with

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Associated With

In most of my rails applications I need to check if a certain object is associated with another object. I usually do this the long way, so I finally decided to create a plugin for a project I'm working on called Scrum&Cola

Example

Ok, first you'll need ActiveRecord classes, with associations.

class Post < ActiveRecord::Base
  belongs_to :author
end

class Author < ActiveRecord::Base
   has_many :posts
end

Next find a post, and an author.

post = Post.find(1)
author = Author.find(1)

Now lets see if they're associated.

post.associated_with? author

or I can do it the other way around

author.associated_with? post

This will return a true or false!

Tests

To run the tests, create a database associated_with_test and simply just into the plugin's directory and type rake

TODO

I still have to write tests for polymorphic relationships, but i've "tested" manually and it works fine.. expect them up soon.

Copyright (c) 2010 Mark Gandolfo, released under the MIT license

About

Ruby on Rails check if object1 is associated object2

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages