0
project1: contains app\models\foo.rb (ActiveRecord facade for foo object)
project2: I would like to fetch all foo's in project2.

How do I reference foo from project1 over here, so that I can avoid code duplication corresponding to fetching information from the same table?

Jason
  • 12,229
  • 20
  • 51
  • 66

2 Answers2

3

If you want to share data then ActiveResource should do the trick. I'd recommend watching the Railscast to get you started.

If it's just the class code you're trying to share, then I'd say just put it in a gem.

aNoble
  • 7,033
  • 2
  • 37
  • 33
  • I am a newbie, is there a sample gem I could take a look at to achieve code sharing? – Jason Oct 11 '11 at 10:32
  • 1
    You could create your own gem, it's really easy with engines in Rails 3.1. Just go to a new directory and run `rails plugin new my_gem_name --full`. Edit your .gemspec file and then develop it like a regular Rails app. Run `gem build` to create a `.gem` file you can install and include in your Gemfile or just use `gem 'my_gem_name', :path => /path/to/my_engine`. – aNoble Oct 11 '11 at 15:24
0

I think there are 2 approaches to do that:

  1. Use 2 databases in one Rails app. There are some related questions on stackoverflow. See as example How do i work with two different databases in rails with active records?
  2. Use the Rails ActiveResource to do that. It is a lightweight approach, to use external resources by a REST interface. However, you drive 2 applications, and have calls between them. Depending on the operating system, this may cost more runtime than the other approach.
Community
  • 1
  • 1
mliebelt
  • 15,345
  • 7
  • 55
  • 92