Polymorphic Associations in Rails3

ror31In polymorphic associations, a model can belong to more than one model, on a single association.

Here is an example where a model is associated with two other models in Rails3. For example we have Events and Article model which have comments. So the “Comment” model is common between the “Event” and “Article” model. Here are the steps to implement the polymorphic association between these three models.

Step#1
Let’s create a resource “Comment” by rails generator.

rails g scaffold Comment content:text

Step#2

Add associations in the models as below

Comment model:

class Comment < ActiveRecord::Base
attr_accessible :commentable_id, :commentable_type, :content
belongs_to :commentable, :polymorphic => true  #Now, it is acted as polymorphic
end

Event model:

class Event < ActiveRecord::Base
attr_accessible :name
has_many :comments, :as => :commentable
end

Article model:

class Article < ActiveRecord::Base
attr_accessible :name
has_many :comments, :as => :commentable
end

Step#3

Add the following attributes in the migration files of the comment model

Look for newly created file under “db/migrate” folder

class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.text :content
t.references :commentable, :polymorphic => true
t.timestamps
end
end
end

Then execute rake db: migrate in the command line

Step#4

Add nested resources inside the “config/routes.rb” file

resources :events  do
resources :comments
end
resources :articles do
resources :comments
end

Step#5

Add link to add new comments in view page of article and events as follows

In “/app/views/events/show.html.erb”

<%= link_to 'New comment', new_event_comment_path(@event)  %>

In /app/views/articles/show.html.erb

<%= link_to 'New comment', new_article_comment_path(@article)  %>

Step#6

Changing the form_for tag in new comment page

In “/app/views/comments/_form.html.erb”

Before

<%= form_for (@comment) do |f| %>

After

<%= form_for [@commentable, @comment] do |f| %>

Add following codes in both “Articles” & “Events” controllers to get the comments individually

In “/app/controllers/events_controller.rb”

def show
@event = Event.find(params[:id])
@comments= @event.comments #added to view all the comments for the selected event
respond_to do |format|
format.html # show.html.erb
format.json { render json: @event }
end
end

In “/app/controllers/articles_controller.rb”

def show
@article = Article.find(params[:id])
@comments= @article.comments #added to view all the comments for the selected article
respond_to do |format|
format.html # show.html.erb
format.json { render json: @article }
end
end

Step#8

Add the following codes to “Comments” controller to creating a comment

In “/app/controllers/comments_controller.rb”

class CommentsController < ApplicationController
def new
@commentable = find_commentable
@comment = Comment.new
end
def create
@commentable = find_commentable
@comment = @commentable.comments.build(params[:comment])
if @comment.save
flash[:notice] = "Successfully created comment."
redirect_to :id => nil
else
render :action => 'new'
end
end
 
private
 
def find_commentable
params.each do |name, value|
if name =~ /(.+)_id$/
return $1.classify.constantize.find(value)
end
end
nil
end
end

Now the comment model will work as a polymorphic association between article and event model.

The Orangescrum Feature Summary

Constant updates to Orangescrum takes place as the engineers keep on adding new features almost every day. With these new features, your project management will now be simpler, faster, and transparent. Here are a few added:

Case Templates: Create templates for repetitive cases and choose them while posting cases.

Project wise Case Search: Search cases project-wise for effective easy case tracking & project management.

Case Glide Chart: A graphical chart to track bugs, enhancements, development, R & D, Quality Assurance, Maintenance and upgradations during a certain period in the project timeline.

Case Glide Chart

File or Case Archiving: You can archive files or cases not used anymore and also restore them back when needed.

Image Viewer and Direct Image Links: View attached images in your projects through the pop-up image viewer. Also, view images on Orangescrum from direct mail links and download them.

Image Viewer and Direct Image Links

Improved Email Notifications: You can now set preferences for receiving email notifications to admin or team members regarding projects/milestones etc.

Improvised Milestones Management: Keep track of overdue and upcoming milestones.

Improvised Milestones Management

(OPTIONAL)

We’re on the process of adding some new features like calendars, Improved Time Tracking, and Project Report Generation to our future updates.

Keep visiting the space for more updates.

How To Implement Security Patches On Ruby On Rails Applications

The security of any software product or application is something that every software solution provider is wary of. It is no different in the case of a Ruby on Rails application.

Even after an application is successfully developed and deployed, there can be possibilities for many unseemly activities.

US-based high-end software solution provider Andolasoft.Inc sheds some light on some of the basic precautions that are worked upon before deploying an application.

The Company’s Ruby on Rails developers pay special attention to security issues in every undertaken venture.
Authentication and authorization of the User model are two different things.

Never miss an update from us. Join 10,000+ marketers and leaders.

Actions like create/update/delete always need to be protected. Even popular plug-ins like Restful authentication and Clearance only authenticate whereas providing no helper methods for authorization.

Mass assignment of sensitive attributes from re-assignment should be protected.

This can be taken care of by disabling mass assignment with the help of attr_accessible or attr_protected in the respective models. Attributes like account_id’s and user_id’ should be protected.

Also disabling updates on protected attributes can be by using attr_readonly so that the Ids don’t get updated once set.

Content types of files being uploaded can be faked, so uploaded files should be placed in protected directories or servers.

Also, file extensions should be checked and the web-server should be disabled so as not to execute such files in the uploaded directories.

Sessions are prone to vulnerabilities like hijacks, replaying cookies, and session fixation. Extra caution must be taken regarding storing data in sessions.

Active record Objects should never be stored in sessions which might change that Object’s behavior while migration and future code change.

The best practice is to only store the ids of the records. Also, sensitive data like User access privileges and money based information should not be stored in sessions.

What Makes Offshore Software Development So Popular?

 

If you are looking for technical business solutions without creating a hole in your pocket, then offshore software development just fits the bill.

Be it standalone bespoke technical solutions to per-user oriented or multifaceted, corporate solutions; offshore software service providers understand the client’s needs and expectations and develop solutions accordingly.

Faster communication methods and timing flexibility have created a stirring impact on offshore software development companies.

As these companies are bestowed with skilled manpower and the best technological resources, clients from all over the world want to reap profits from their limited investments.

As per the latest survey, more than 90% of Fortune 500 companies outsource a certain part of their operations to offshore software development companies. And today, outsourcing in itself is a 60 billion dollar industry.

Cheap labor is not the only parameter to choose an offshore solution provider.

Specialized knowledge and expertise, knowledge of the clients’ industry, and experience of handling complex projects are some other factors that will ensure that you receive an excellent return on your investment.

Andolasoft provides a high-quality offshore software development service by leveraging the best in people, technologies, and processes.

The company is dedicated to reduce the operational cost of your company and to give it a technological advantage over your competitors.

Our technical experts are there to take care of your technical issues so that you can devote more time to your core business.

Over the years, the company has provided solutions par excellence to its global clientele. For an effective, cost-efficient, and flexible offshore software development service, Andolasoft is your trusted choice.

Custom Software Development & Its Efficiency

Gone are the days, when software development used to be a time-consuming task. Today, the concept of custom development has made the task pretty easier & faster.

You just need to give your specifications to custom software development solution providers and they will develop solutions for you.

Many businesses especially those who are not into core IT, can be hugely benefited by this.

While custom development takes care of your business needs, you can concentrate on your core business.

What makes custom web application development so much in demand?

The first one is cost factor. Instead of spending money on manual development, you can get the same job done in one-third price. The second factor is time. The longer the time period, the more money will be used in the development process.

Custom development allows any project to complete within the earliest possible time and thus saves a lot of money.

Looking for qualitative, cost-effective, and timely delivered development services? Then our custom software development service fits the bill.

Andoalsoft being a premier offshore development company in India; offers customized solutions according to the clients’ business needs.

We take pride in developing unmatched customized solutions by using the latest and advanced technologies.

We have a large pool of technically-sound developers to meet the escalating demands of the potential clients.

Starting from the desktop application, database design to enterprise applications, and high-end client-server applications; our custom development service covers it all.

 

Offshore Services – You Can Have Your Cake & Eat It Too!

Before the advent of off-shoring services, small and medium business units had a hard time meeting the IT necessities.

They were confronted with a lack of resources, be it staff, deployment issues, operational challenges, or budget specific issues.

But off-shoring with its ready-to-use methodologies, service, and support facility has now addressed all the requirements of SMBs. No wonder, off-shoring is a big boom these days.

Today off-shoring is considered a holistic approach to IT strategy.

Affordable and quality service, trust, and data security are some of the catalysts which prompt many business owners to opt for Indian offshoring companies.

Andolasoft is a fast-growing outsourcing provider in the same arena. Andolasoft’s offshore software development services have been conceptualized to give you the best benefits in areas of information and technology.

Our IT strategy and architecture can definitely leverage your business and facilitate ways to generate more profit. You can even make the geographic expansion of your business in other countries without physical office-setup.

Our off-shoring service will give you access to technical expertise that you might not have inside your own company. While we take care of your technical requirements, you can focus on your company’s core business and competences.