Reputation: 15384
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
Upvotes: 5
Views: 3995
Reputation: 39568
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.
Upvotes: 7