ruby on rails - Is it correct architecture for adding user's posts? -


create action in posts controller:

  def create     @post = current_user.posts.build(post_params)     if @post.not_exists?(current_user)       if @post.save         #flash         redirect_to root_path       else         #flash[:error]         redirect_to root_path       end     else       #flash[:error]       redirect_to root_path     end   end 

post model:

class post < activerecord::base   belongs_to :user    ##validations     def not_exists?(user)     return true unless user.posts.find_by(name: self.name)   end  end 

my question: correct build create action this? or there better architectural design? think fat action.

why not use validation instead ?

class post < activerecord::base   belongs_to :user    validates_uniqueness_of :name, :scope => :user_id 

Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -