5

Not sure if im overthinking this but would like some guidance and advice on this scenario.

I have two applications, one which you can log into and perform your basic CRUD, i.e create blog posts and the second a view of the same application, but no ability to log into and no ability to create a blog post. The second application would read from the same database as the first.

My question is how do i get the two applications reading from the same model in development and do i still need to create my models with columns etc in the view only app?

Example

App 1 (With CRUD)

class Post < ActiveRecord::Base
 extend FriendlyId
 friendly_id :title, use: :slugged

 belongs_to :category
 belongs_to :user
 has_many :images, as: :imageable, :dependent => :destroy

 accepts_nested_attributes_for :images
 attr_accessible :comments, :title, :category_id, :user_id, :image_id, :images_attributes, :imageable_id, :imageable_attributes, :slug


#Validations
validates :comments, :presence => {:message => 'Add your Comments'}
validates :title, :presence => {:message => 'Add your Title'}

#scopes
scope :latest_posts, :order => "posts.created_at DESC"
#scope :ruby_posts, :include => :category, :conditions => {"categories.name" => "Ruby"}, :order => "posts.created_at DESC"


def self.search(search)
  where("title like ?", "%#{search}%")
end

end

App 2 (No Crud)

class Post < ActiveRecord::Base
#do i still provide all associations and attributes here?
end

I would really appreciate an explanation of what is going on in this kind of setup

thanks

1 Answer 1

7

You will need to have your models either shared or duplicated between the two applications. This means your Post example for App 2 would need to have the associations, scopes, and methods.

I have done this once before by moving all of the model classes into a gem that is included into both projects. This was actually pretty easy to do.

You do not need to share migrations though. If they are pointing to the same database, migrations should only live in one app, probably the one doing the writing. I wouldn't even let db/schema be checked in on App 2 and would maybe go further and disable rake db:* tasks.

Even if you move your models into a shared gem, you might want your "read only" app to enforce its read-only behavior by clearing permissions to assign attributes (attr_accessible and accepts_nested_attributes_for) or somehow preventing ActiveRecord models from saving in its environment. One quick and dirty way would be to monkey patch ActiveRecord::Base#save in an initializer for App 2 and have it do nothing or raise an error.

Sign up to request clarification or add additional context in comments.

8 Comments

so just to clarify in my head, in app 2 i still go through the motions of creating the models and setting up the associations and scopes etc like you said. Dont run any migrations as we are reading from app 1. how do i share the database in development though?
@Richlewis conceptually that sounds correct. Though, I highly recommend moving your model classes to a shared gem so you don't have to write them twice. In development, you just need to make sure the config/database.yml of both apps point to the same development database. After creating a table in App 1 through a migration, App 2 will see it so long as the right model class is present.
hmmmm shared gem, not done that before :( like the idea of it though as saves creating everything twice
@Richlewis its not as hard as it sounds. There are plenty of simple guides for creating gems. You can also have bundler create a skeleton gem for you: bundle gem project-models will create a new gem. Then add something like gem "project-models", path: "path/to/gem/dir" in each project's Gemfile.
ok cool, ill look into it, thanks for explaining things, the setup makes a bit more sense now... last question, I take it i can choose which models to put into the gem as opposed to putting them all in there, as an example in app 1 there is a user table, but in app2 i dont need that
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.