Need help creating a GET route - json

I need help implement a route to fetch all blog posts by author_ids.
The post that we're fetching needs to have at least one of the authors specified in the passed in author_ids parameters. I've created a helper function to help me fetch all blog posts by their ID, Post.get_posts_by_user_id
I also need to sort the posts by query parameters given. Also, I need to delete any duplicated posts as efficiently as possible.
I'm stumped here because of the way author_ids is being given. (I'm extremely new to Ruby)
This is what we should get returned from the route: "posts": [{"id": 1, "likes": 960, "popularity": 0.13, "reads": 50361, "tags": ["tech", "health"], text": "Some text here."}, ... ]
Query parameters expected to be given to this route
Update:
After creating the index method, it seems that it is only getting one post rather than getting all posts that are associated with the passed in authorIds.
def index
posts = current_user
.posts
.where(id: params[:authorIds].split(','))
.order(sort_column => sort_direction)
if posts
render json: { post: posts }, status: :ok
else
render json: {error: posts.errors}, status: :unprocessable_entity
end
end
Test cases
Update 2:
Post Model:
class Post < ApplicationRecord
# Associations
has_many :user_posts
has_many :users, through: :user_posts, dependent: :destroy
# Validations
validates :text, presence: true, length: { minimum: 3 }
validates :popularity, inclusion: { in: 0.0..1.0 }
def tags
if super
super.split(",")
end
end
def tags=(value)
if value.kind_of? Array
super value.join(",")
else
super value
end
end
def self.get_posts_by_user_id(user_id)
Post.joins(:user_posts).where(user_posts: { user_id: user_id })
end
end
User Model:
class User < ApplicationRecord
has_secure_password
# Associations
has_many :user_posts
has_many :posts, through: :user_posts, dependent: :destroy
# Validations
validates :username, :password, presence: true
validates :password, length: { minimum: 6 }
validates :username, uniqueness: true
end
User_post Model:
class UserPost < ApplicationRecord
belongs_to :user
belongs_to :post
end

I would do it like below.
def index
author_ids_array = params[:ids].to_s.split(',')
Post
.get_posts_by_user_id(author_ids_array)
.order(sort_column => sort_direction)
end
private
def sort_column
allow_list = %w[id reads likes popularity]
params[:sortBy].presence_in(allow_list) || allow_list.first
end
def sort_direction
allow_list = %w[asc desc]
params[:direction].presence_in(allow_list) || allow_list.first
end

Related

How to send notifications to all commentors on polymorphic comments in rails?

I am using polymorphic association for comments.
I want to send notification to all user who comments on post.
Suppose user1 posted a post.
User2 comments on it.
User1 gets
notification (users = User.joins(:posts).where(:posts => {id: params[:post_id]})) that "User2 posted a comment"
Again, User1 comments.
Now, User2 should get a notification (users= ????)
If user2, user3, user4 comments then all the related users should get notifications.
comment.rb
class Comment < ApplicationRecord
belongs_to :user
belongs_to :commentable, polymorphic: true
has_many :comments, as: :commentable
validates :comment, presence: true
after_create :notifications
def notifications
#Create Notification
users = ????
(users-[#current_user]).each do |user|
Resque.enqueue(NotifyJob, Notification.create(
recipient: user,
actor: #current_user,
action: "posted",
notifiable: #comment))
end
end
end
user.rb
class User < ApplicationRecord
has_many :posts, dependent: :destroy
has_many :comments, as: :commentable, dependent: :destroy
has_many :notifications, foreign_key: :recipient_id, dependent: :destroy
end
post.rb
class Post < ApplicationRecord
belongs_to :user
validates :comment, presence: true
validates :user_id, presence: true
has_many :comments, as: :commentable
end
comments_controller.rb
module Api
module V1
class CommentsController < ApplicationController
skip_before_action :verify_authenticity_token
before_action :authorize_request
before_action :find_commentable
def new
#comment = Comment.new
end
def create
#comment = #commentable.comments.new(comment_params)
#comment.user = #current_user
if #comment.save
render json: #comment, status: :created
else
render json: { errors: #comment.errors.full_messages },
status: :unprocessable_entity
end
end
def show
#comment = Comment.find(params[:id])
if !#comment.nil?
render json: #comment, status: :ok
else
render json: {errors: #comment.errors.full_messages}, status: :unprocessable_entity
end
end
private
def comment_params
params.permit(:comment)
end
def find_commentable
#commentable = Comment.find_by_id(params[:comment_id]) if params[:comment_id]
#commentable = Post.find_by_id(params[:post_id]) if params[:post_id]
end
end
end
end
Your first problem is in User model. You have
has_many :comments, as: :commentable, dependent: :destroy
but Comment model belongs_to :user and have user_id, it means that in User should be
has_many :comments, dependent: :destroy
In this case you can get all user comments easily with User.first.comments
The second one is callback. Vyacheslav Loginov is right about bad practice to put this complex logic inside the controller action, but callbacks are not good practice too. Not sure which of them is worse. You will create notification on every comment creation, even from console. Notifications will be created in every test setup where you create commnets. Not sure if that it is what you really want.
Better option is to create separate ServiceObject to handle notifications
class CommentsController < ApplicationController
def create
#comment = #commentable.comments.new(comment_params)
#comment.user = #current_user
if #comment.save
NotificationSender.new(#comment).comment_notification
render json: #comment, status: :created
else
render json: { errors: #comment.errors.full_messages },
status: :unprocessable_entity
end
end
end
class NotificationSender
def initialize(resource)
#resource = resource
end
# you can add here all other notifications as methods
def comment_notification
users = User.where(id: #resource.commentable.comments.pluck(:user_id)).
where.not(id: #resource.user_id)
users.each do |user|
Resque.enqueue(
NotifyJob,
Notification.create(
recipient: user,
actor: #resource.user,
action: "posted",
notifiable: #resource
)
)
end
end
end
I didn't make much changes. I just made changes like this on:
comment.rb
class Comment < ApplicationRecord
belongs_to :user
belongs_to :commentable, polymorphic: true
has_many :comments, as: :commentable
validates :comment, presence: true
private
after_commit :create_notifications, on: [:create]
#Create Notification
def create_notifications
commentable = self.commentable
commentors= Comment.where(commentable: commentable).pluck(:user_id)
users = User.find(commentors)-[self.user]
users.each do |user|
Resque.enqueue(NotifyJob, Notification.create(
actor: self.user,
recipient: user,
action: "posted",
notifiable: self.commentable))
end
end
end

Adding current location to Ruby on Rails app with photo_geoloader

So I've sent about 3 days researching geoip, geocoder, cookies, sessions, html5 and so forth.
I am trying to add a current location (as of the location when the user posts the photo in form). The user will not change this data, I just want it to be automatically added to the post, and then added to the view of the post (currently i am using .time_ago for the timestamp of the post). I can currently show in a map, but am lost as to adding it to the post.
I prefer to user HTML5 or
rails #lat_lng = cookies[:lat_lng].split("|").
I just need it printed to the post of each user.
I have tried using Exifr gem to extract that data but it is not precise enough. I am interested also in photo_geoloader gem yet there is little documentation.
So far i have created a model using "photo" as my model name. Currently I am validating the uploaded image through my "post" model;
class Post < ActiveRecord::Base
acts_as_votable
belongs_to :model
has_many :comments, dependent: :destroy
has_many :notifications, dependent: :destroy
validates :model_id, presence: true
validates :image, presence: true
validates :caption, length: { minimum: 3, maximum: 300 }
has_attached_file :image,
styles: lambda { |a| a.instance.check_image_type}
def check_image_type
if is_image_type?
{:medium => "640"}
elsif is_video_type?
{
:medium => { :geometry => "640x480", :format => 'flv'},
:thumb => { :geometry => "100x100#", :format => 'jpg', :time => 10}, :processors => [:transcoder]
}
else
{}
end
end
def is_image_type?
image_content_type =~ %r(image)
end
def is_video_type?
image_content_type =~ %r(video)
end
validates_attachment_content_type :image, :content_type => [/\Aimage\/.*\Z/, /\Avideo\/.*\Z/, /\Aaudio\/.*\Z/]
scope :of_followed_models, -> (following_models) { where model_id: following_models }
end
I have researched everything so I'm looking for a full process in rails 5 :)

Delete an element from a collection

I'm facing to a stupid problem. I have created a collection select which is creating elements into a join table "staffs_task" to reference an association between the model staff and task.
And now I would like two things: (1) a button delete this association (2) and a little bit of code for my model staffs_task to avoid duplication, so with the task_id and staff_id. And last info, task is a model built by ranch
my code:
(the collection in new_task)
<%= select_tag "staffs_task", options_from_collection_for_select(#staffs, 'id', 'name') , :multiple => true %>
(task_controller)
skip_before_action :configure_sign_up_params
before_action :set_ranch
before_action :set_task, except: [:create]
def create
#task = #ranch.tasks.create(task_params)
#staffs = Staff.where(:id => params[:staffs_task])
#task.staffs << #staffs
if #task.save
#task.update(done: false)
#task.update(star: false)
flash[:success] = "The task was created "
else
flash[:success] = "The task was not created "
end
redirect_to #ranch
end
private
def task_params
params.require(:task).permit(:content, :deadline, :row_order, :date, :assigned_to)
end
def set_ranch
#ranch = Ranch.find(params[:ranch_id])
end
def set_task
#task = #ranch.tasks.find(params[:id])
end
So if you have any idea about one of this two things, your help would be welcome
Thanks in advance !!
Lets say you have the following many to many setup with a join model:
class Staff
has_many :assignments
has_many :tasks, through: :assignments
end
class Task
has_many :assignments
has_many :staff, through: :assignments
end
class Assignment
belongs_to :task
belongs_to :staff
end
Note that the plural of staff is staff - unless you are talking about the sticks carried by wizards.
ActiveRecord creates "magical" _ids setters for all has_many relationships. When used with a has_many through: relationship rails is smart enough to just remove the rows from the join table.
You can use this with the collection_select and collection_checkboxes methods:
<%= form_for([#task.ranch, #task]) do |f| %>
<%= f.collection_select(:staff_ids, Staff.all, :id, :name, multiple: true) %>
<% end %>
You would then set your controller up like so:
def create
#task = #ranch.tasks.new(task_params) do |t|
# this should really be done by setting default values
# for the DB columns
t.done = false
t.star = false
end
if #task.save
redirect_to #ranch, success: "The task was created"
else
render :new, error: "The task was not created"
end
end
private
def task_params
params.require(:task)
.permit(:content, :deadline, :row_order, :date, :assigned_to, staff_ids: [])
end
staff_ids: [] will allow an array of scalar values. Also not that .new and .create are not the same thing! You where saving the record 4 times if it was valid so the user has to wait for 4 expensive write queries when one will do.

Rails - Issue with controllers for creating a user group

I currently have a Rails app that allows users to create a group and allows other users to join the group. The group "creator" is the owner of the group and any that join ON REQUEST are the members. I want a user to be able to create only one group, but belong to many (I think that I've captured that relationship, but I'm a little uncertain). I need a little help understanding what I need to do to show the group associations on the User's page. How should I go about creating a group "show" page and how do I show the group memberships on the User "show" page? I got help from SO and followed the Railscast on self-referential association to help guide me through setting up the relationships.
In this example groups are called Cliqs and membership is controlled by a has_many :through. I used Devise for the User model.
To clarify my question: Am I capturing the relationship that I'm trying to set up? How would I go about allowing the user to view groups that they belong to?
As an aside, I'm not sure if the group creator is being associated as a member of the group. How do I represent that in my model/controller?
Here is my code:
Group Model:
class Cliq < ActiveRecord::Base
belongs_to :owner, class_name: 'User'
has_many :members, through: :cliq_memberships, source: :user
has_many :cliq_memberships
end
Membership Model:
class CliqMembership < ActiveRecord::Base
belongs_to :cliq
belongs_to :user
end
User Model:
class User < ActiveRecord::Base
has_one :owned_group, foreign_key: 'owner_id', class_name: 'Group'
has_many :cliqs, through: :cliq_memberships
has_many :cliq_memberships
.
.
.
end
Group Controller:
class CliqsController < ApplicationController
def show
#cliq = Cliq.find(params[:id])
end
def new
#cliq = Cliq.new(params[:id])
end
def create
#cliq = Cliq.create(cliq_params)
if #cliq.save
redirect_to current_user
else
redirect_to new_cliq_path
end
end
def destroy
end
def cliq_params
params.require(:cliq).permit(:name, :cliq_id)
end
end
Group Membership Controller:
class CliqMembershipsController < ApplicationController
def create
#cliq = cliq.find(params[:cliq_id])
if #cliq_membership.save = current_user.cliq_memberships.build(:cliq_id => params[:cliq_id])
flash[:notice] = "Joined #{#cliq.name}"
else
#Set up multiple error message handler for rejections/already a member
flash[:notice] = "Not able to join Cliq."
end
redirect_to cliq_url
end
def destroy
#cliq = Cliq.find(params[:id])
#cliq_memberships = current_user.cliq_memberships.find(params[cliq_memberships: :cliq_id]).destroy
redirect_to user_path(current_user)
end
end
And my User Show Page:
<h1> <%= #user.username %> </h1>
<h2>Cliqs</h2>
<%= link_to "Create Cliq", new_cliq_path %>
<ul>
<% for cliq_membership in #user.cliq_memberships %>
<li>
<%= cliq_membership.cliq.name %>
(<%= link_to "Leave Cliq", cliq_membership, :method => :delete %>)
</li>
<% end %>
</ul>
<h3>Title:</h3>
<% #uploads.each do |upload| %>
<div>
<%= link_to upload.title, upload_url %>
</div>
<% end %>
And my Migrations:
Cliq:
class CreateCliqs < ActiveRecord::Migration
def change
create_table :cliqs do |t|
t.string :name
t.references :owner
t.integer :cliq_id
t.timestamps null: false
end
end
end
CliqMemberships:
class CreateCliqMemberships < ActiveRecord::Migration
def change
create_table :cliq_memberships do |t|
t.references :user
t.references :cliq
t.timestamps null: false
end
end
end
FULL SOLUTION OF WHAT WORKED BELOW.
Try the following:
Your revised models. Fixed the following issues:
In User model, for has_one :owned_group, you set class_name as Group instead of Cliq.
Declare has_many before has_many :through. It may work otherwise, but it is a good practice and easy for readability.
class User < ActiveRecord::Base
has_one :owned_group, foreign_key: 'owner_id', class_name: 'Cliq'
has_many :cliq_memberships
has_many :cliqs, through: :cliq_memberships
end
class CliqMembership < ActiveRecord::Base
belongs_to :cliq
belongs_to :user
end
class Cliq < ActiveRecord::Base
belongs_to :owner, class_name: 'User'
has_many :cliq_memberships
has_many :members, through: :cliq_memberships, source: :user
end
Your revised controllers. Fixed the following issues:
In the CliqsController, as it is relates to Cliq, you won't get cliq_id while creating it. So removed the cliq_id from the cliq_params. You could add other cliq related attributes in there.
In create, you forgot to assign the current_user as the owner of the cliq. This is addressed by the next note.
As the user is the owner of the cliq, built the cliq using build_owned_group which automatically sets the current_user as the owner.
Try not to do multiple things in the same statement. Like assigning it to a variable as well as doing some operation on the newly assigned variable. For example: In create action of CliqMembershipsController, you were assigning the #cliq_membership as well as calling save on it. Separated those two into two steps.
In destroy of CliqMembershipsController, there is no need to load the #cliq and also fixed the way you are finding the #cliq_membership.
class CliqsController < ApplicationController
def show
#cliq = Cliq.find(params[:id])
end
def new
#cliq = Cliq.new(params[:id])
end
def create
#cliq = current_user.build_owned_group(cliq_params)
if #cliq.save
redirect_to current_user
else
redirect_to new_cliq_path
end
end
private
def cliq_params
params.require(:cliq).permit(:name)
end
end
class CliqMembershipsController < ApplicationController
def create
#cliq = Cliq.find(params[:cliq_id])
#cliq_membership = current_user.cliq_memberships.build(cliq: #cliq)
if #cliq_membership.save
flash[:notice] = "Joined #{#cliq.name}"
else
#Set up multiple error message handler for rejections/already a member
flash[:notice] = "Not able to join Cliq."
redirect_to cliq_url
end
def destroy
#cliq_membership = current_user.cliq_memberships.find(params[:id])
if #cliq_membership.destroy
redirect_to user_path(current_user)
end
end
end
And finally your revised view:
Fixed few things.
Try to use each on the collection to iteration through. This is more ruby way, instead of for loop.
Based on your CliqMemberhipsController code, I assumed you are using nested resources as below. So fixed the link_to to use cliq_cliq_memberhip_path instead of cliq_membership_path.
<h1><%= #user.username %></h1>
<h2>Cliqs</h2>
<%= link_to "Create Cliq", new_cliq_path %>
<ul>
<% #user.cliq_memberships.each do |cliq_membership| %>
<li><%= cliq_membership.cliq.name %>(<%= link_to "Leave Cliq", cliq_cliq_membership_path([cliq, cliq_membership]), method: :delete %>)</li>
<% end %>
</ul>
This assumes you have a routes file with the following:
resources :cliqs do
resources :cliq_memberships
end
Going along the lines of my comment above it seems to me the best thing to do is implement some kind of role attribute in the bridge table.
The Rails docs say this:
You should use has_many :through if you need validations, callbacks, or extra attributes on the join model.
So you might try this in your models:
class Cliq < ActiveRecord::Base
has_many :cliq_memberships
has_many :members, through: :cliq_memberships
def owner
cliq_memberships.where(role: 'owner').user
end
end
# this model is used to access attributes on the bridge table
class CliqMembership < ActiveRecord::Base
belongs_to :cliq
belongs_to :user
attr_accessor :role
end
class User < ActiveRecord::Base
has_many :cliq_memberships
has_many :cliqs, through: :cliq_memberships
# something like this would make it easy to grab the owned cliq
def ownedCliq
cliq_memberships.where(role: 'owner').cliq
end
end
so the bridge table stores role which would be an enum or a string representing 'member', 'owner', and maybe 'admin' or something.
Some example usage:
# say I have a user
u = User.find(1)
# and I want the cliq that he/she owns
owned_cliq = u.ownedCliq
# maybe I have a group:
g = Cliq.find(1)
# and I want the user that owns it:
my_owner = g.owner
# now let's get all the members of the cliq (including the 'owner')
my_members = g.members
More example usage:
# inside the controller...
# say I have a user:
u = User.find(1)
# this user is trying to create a cliq
# pretend we fill it in with its data here...
c = Cliq.new
c.save!
# we'll need to hook the two together:
cm = CliqMembership.new(role: 'owner', user_id: u.id, cliq_id: c.id)
cm.save!
# or we might try something like this:
#cm = CliqMembership.find_or_create_by #...
Also, I found this SO answer which does a good job of explaining things.
So I started with the code in my question above and then worked inward to my answer (through many additional trials). This may help someone in the future so here is what worked. (Taking advice from both answers):
class Cliq < ActiveRecord::Base
belongs_to :owner, class_name: 'User'
has_many :cliq_memberships
has_many :members, through: :cliq_memberships, source: :user
end
class CliqMembership < ActiveRecord::Base
belongs_to :cliq
belongs_to :user
end
class User < ActiveRecord::Base
has_one :owned_cliq, foreign_key: 'owner_id', class_name: 'Cliq'
has_many :cliq_memberships
has_many :cliqs, through: :cliq_memberships
.
.
.
end
class CliqsController < ApplicationController
def show
#cliq = Cliq.find(params[:id])
end
def new
#cliq = Cliq.new(params[:id])
end
def create
#cliq = current_user.build_owned_cliq(cliq_params)
#cliq.members << current_user
if #cliq.save
redirect_to current_user
else
redirect_to new_cliq_path
end
end
def destroy
end
def cliq_params
params.require(:cliq).permit(:name, :cliq_id)
end
end
class UsersController < ApplicationController
def show
#find way to use username instead of id (vanity url?)
#user = User.find(params[:id])
#uploads = Upload.all
#cliq_memberships = CliqMembership.all
#cliqs = Cliq.all
end
end
class CliqMembershipsController < ApplicationController
def show
end
def create
#cliq = Cliq.find(params[:cliq_id])
#cliq_membership = current_user.cliq_memberships.build(cliq: #cliq)
if #cliq_membership.save
flash[:notice] = "Joined #{#cliq.name}"
else
#Set up multiple error message handler for rejections/already a member
flash[:notice] = "Not able to join Cliq."
end
redirect_to cliq_url
end
def destroy
#cliq_membership = current_user.cliq_membership.find(params[:id])
if #cliq_membership.destroy
redirect_to user_path(current_user)
end
end
class CreateCliqs < ActiveRecord::Migration
def change
create_table :cliqs do |t|
t.string :name
t.references :owner
t.timestamps null: false
end
end
end
class CreateCliqMemberships < ActiveRecord::Migration
def change
create_table :cliq_memberships do |t|
t.references :user
t.references :cliq
t.timestamps null: false
end
end
end
Thanks so much for all of the incredible help on this thread!

Rails create and update array in database

So I have a service object that submits up votes and voted users into a database:
Here's the posts model:
class Post < ActiveRecord::Base
attr_accessible :comment_count, :downvote, :id, :text, :title, :upvote, :url, :user_id, :users_voted_up_by, :users_voted_down_by
serialize :users_voted_up_by, Array
serialize :users_voted_down_by, Array
belongs_to :user
end
Here's the User model:
class User < ActiveRecord::Base
attr_accessible :email, :password, :password_confirmation, :username, :good_karma, :bad_karma, :posts_voted_up_on, :posts_voted_down_on
serialize :posts_voted_up_on, Array
serialize :posts_voted_down_on, Array
has_many :posts
attr_accessor :password
before_save :encrypt_password
validates_confirmation_of :password
validates_presence_of :password, :on => :create
validates_presence_of :email
validates_uniqueness_of :email
validates_presence_of :username
validates_uniqueness_of :username
def self.authenticate(email, password)
user = find_by_email(email)
if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
user
else
nil
end
end
def encrypt_password
if password.present?
self.password_salt = BCrypt::Engine.generate_salt
self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
end
end
end
Now here's my Voter class that controls up votes and down votes.
class Voter
def initialize(post, user)
#post = post
#user = user
end
def upvote
return false unless #post.users_voted_up_by
#post.upvote += 1
#post.users_voted_up_by << #user.username
#user.good_karma += 1
#post.save && #user.save
end
def downvote
return false unless #post.users_voted_down_by
#post.upvote += 1
#post.users_voted_down_by << #user.username
#user.bad_karma += 1
#post.save && #user.save
end
end
It adds and retrevies the first one fine:
User1
But when I perform another "upvote" with a different user, instead of adding it to the array it just adds it to the string like this:
User1User2
Am I not using the line correctly?
#post.users_voted_up_by << #user.username
You're trying to model a many-to-many relationship via serialized arrays. In my opinion this is a misuse of Rails serialization feature.
The more appropriate way to design your problem is to create a Vote model which belongs to User and has one Post. It is better both in design perspective and db-performance perspective.
Once you do that you won't add users to a post votes array, you will just create a new Vote instance with the voter (user) id and the post id.