Paperclip setup nightmare. advice? - paperclip

We have projects and photos in separate tables and models. Here is our schema:
create_table "projects", force: :cascade do |t|
t.string "title"
t.integer "price"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "description"
t.boolean "retired", default: false
t.integer "tenant_id"
t.date "requested_by"
t.integer "repayment_rate"
t.date "repayment_begin"
end
create_table "photos", force: :cascade do |t|
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "project_id"
end
In the project model I have:
has_attached_file :image
validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png"]
Is this acceptable as a way to setup paperclip? Or do projects and photos have to be all in one table?

yes, it's acceptable, but your has_attached_file and validates_attachment_content_type must be into photos model, not into project model.
# project.rb
class Project < ActiveRecord::Base
has_many :photos
end
# photo.rb
class Photo < ActiveRecord::Base
belongs_to :project
has_attached_file :image #, styles: { svga: ["800x600!", :jpg], big: ["500x375!", :jpg], small: ["190x143!", :jpg], thumb: ["95x71!", :jpg] }
validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png"] # or do_not_validate_attachment_file_type :image
end

Related

Problems with has_many associations searchkick

I have probelm with use Searchkick with associated models.
I would like to be able to do a search that is able to list all the model information from the List model.
For example
"frederic 2020-01-01"
I would like this to return the fields related to frederic (which in this case is an account_name) and the date, which is related to model post
Can someone help me?
The Models:
class List < ApplicationRecord
searchkick
has_many :people
end
class Person < ApplicationRecord
has_many :accounts
belongs_to :list
searchkick
end
class Account < ApplicationRecord
has_many :posts
belongs_to :person
searchkick
end
class Post < ApplicationRecord
belongs_to :account
searchkick
end
And on List_controller:
class ListsController < ApplicationController
def index
if params[:keywords].present?
#lists = List.search(params[:keywords], operator: "or")
else
#lists = List.all
end
end
end
My schema.rb
create_table "accounts", force: :cascade do |t|
t.string "social_network"
t.string "name_account"
t.integer "person_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["person_id"], name: "index_accounts_on_person_id"
end
create_table "lists", force: :cascade do |t|
t.string "name"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "people", force: :cascade do |t|
t.string "name"
t.integer "list_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["list_id"], name: "index_people_on_list_id"
end
create_table "posts", force: :cascade do |t|
t.string "post_text"
t.date "date"
t.string "link"
t.integer "account_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["account_id"], name: "index_posts_on_account_id"
end
add_foreign_key "accounts", "people"
add_foreign_key "people", "lists"
add_foreign_key "posts", "accounts"
end

has_many through association error while relating users and projects

I have used has_many :through association for many-to-many relationship between two models namely user and project, but it seems as there is some mistake in it, as the queries which worked well in many-to-one relationship is throwing errors. Can someone check it please! The schema file is as below:
ActiveRecord::Schema.define(version: 2018_12_19_170114) do
create_table "project_users", force: :cascade do |t|
t.integer "user_id"
t.integer "project_id"
t.index ["project_id"], name: "index_project_users_on_project_id"
t.index ["user_id"], name: "index_project_users_on_user_id"
end
create_table "projects", force: :cascade do |t|
t.text "content"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "picture"
t.string "title"
t.index ["user_id"], name: "index_projects_on_user_id"
end
create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
t.integer "enroll"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
t.boolean "admin", default: false
t.index ["email"], name: "index_users_on_email", unique: true
end
end
The user.rb file includes the code:
class User < ApplicationRecord
has_many :project_users
has_many :projects, through: :project_users
def feed
projects
end
end
The project.rb file has the code:
class Project < ApplicationRecord
has_many :project_users
has_many :users, :through => :project_users
The project_user.rb file has the code:
class ProjectUser < ApplicationRecord
belongs_to :project, foreign_key: true
belongs_to :user, foreign_key: true
end
class StaticPagesController < ApplicationController
def home
if logged_in?
#project = current_user.projects.build
#feed_items = current_user.feed.paginate(page: params[:page])
end
end
The error is thrown by the code:
<% if #user.projects.any? %>
<h3>Projects (<%= #user.projects.count() %>)</h3>
The error is:
SQLite3::SQLException: no such column: project_users.true: SELECT 1 AS one FROM "projects" INNER JOIN "project_users" ON "projects"."id" = "project_users"."true" WHERE "project_users"."user_id" = ? LIMIT ?
This is a terrible worded question, as the comments have suggested, please include error messages otherwise it's impossible to debug.
At a glance, I can see the following issues in your schema:
Tables are named in plural form, e.g. project_users
Foreign keys are in singular form, e.g. user_id
Use the t.references :user helper instead of t.integer :user_id. You can then pass index: true, foreign_key: true options to setup indices and foreign keys.

couldn't nullify associated child object in rails?

I have a class Trainer associated with many Facilities and many ServiceClasses. I just want to make reference null in facilities and service_classes table when trainer record is destroyed.
Whenever I try trainer.destroy, it shows me errors as I mention in the screenshot.
I also tried callbacks to make reference null before_destroy but it didn't work for me either.
Thanks in advance if someone can guide me, it has already taken me a lot of time.
Trainer:
class Trainer < ActiveRecord::Base
belongs_to :user
has_many :service_classes,dependent: :nullify
has_many :facilities,dependent: :nullify
end
Service Class:
class ServiceClass < ActiveRecord::Base
acts_as_paranoid
mount_uploader :image, EventUploader
belongs_to :trainer
end
Facility Class:
class Facility < ActiveRecord::Base
acts_as_paranoid
belongs_to :user
belongs_to :facility_category
has_many :facility_prices
has_many :facility_details
belongs_to :trainer
end
Schema:
create_table "service_classes", force: :cascade do |t|
t.string "name"
t.text "description"
t.string "image"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "deleted_at"
t.integer "trainer_id"
end
add_index "service_classes", ["deleted_at"], name: "index_service_classes_on_deleted_at", using: :btree
add_index "service_classes", ["trainer_id"], name: "index_service_classes_on_trainer_id", using: :btree
create_table "facilities", force: :cascade do |t|
t.string "name"
t.integer "user_id"
t.integer "facility_category_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "deleted_at"
t.integer "trainer_id"
end
add_index "facilities", ["deleted_at"], name: "index_facilities_on_deleted_at", using: :btree
add_index "facilities", ["trainer_id"], name: "index_facilities_on_trainer_id", using: :btree

Rails 4: Group Text Message Strategy - Passing Multiple Records to a Controller

My Rails app works by letting Users create Clients and then send or schedule text_messages using Twilio. Creating one Client and then generating one text_message record works great. The User owns Clients which owns Text_Message.
My next feature to tackle is to allow a User to choose which Clients they want to send a group text message too. I want to generate a text_message record for each Client (with the same content and scheduled_date). Saved text_messages are getting added to a Sidekiq queue.
I need some help (please) on the strategy on this. How do I implement Group_Text while keeping the code dry and continuing to generate records for the text_message table? How do I pass multiple selected clients from a form/view to a controller to create the records while using the logic from the Text_Message model? Thanks for the pointers!
Relevant code
schema.rb
ActiveRecord::Schema.define(version: 20141207214913) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "action_plans", force: true do |t|
t.integer "client_id"
t.text "description"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "action_plans", ["client_id"], name: "index_action_plans_on_client_id", using: :btree
create_table "clients", force: true do |t|
t.integer "user_id"
t.string "first_name", null: false
t.string "last_name", null: false
t.string "phone", null: false
t.string "salesforce_id", null: false
t.string "email"
t.datetime "created_at"
t.datetime "updated_at"
t.text "contact_id"
end
add_index "clients", ["user_id"], name: "index_clients_on_user_id", using: :btree
create_table "coach_emails", force: true do |t|
t.integer "user_id"
t.string "email", null: false
t.string "coach_firstname"
t.string "client_lastname"
t.string "client_firstname"
t.text "content", null: false
t.boolean "sentstatus"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "coach_emails", ["user_id"], name: "index_coach_emails_on_user_id", using: :btree
create_table "goals", force: true do |t|
t.integer "action_plan_id"
t.text "description"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "goals", ["action_plan_id"], name: "index_goals_on_action_plan_id", using: :btree
create_table "steps", force: true do |t|
t.integer "goal_id"
t.text "description"
t.date "due_by"
t.boolean "complete", default: false
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "steps", ["goal_id"], name: "index_steps_on_goal_id", using: :btree
create_table "text_messages", force: true do |t|
t.integer "client_id"
t.text "content"
t.boolean "incoming_message"
t.datetime "created_at"
t.datetime "updated_at"
t.date "scheduled_date"
t.boolean "sentstatus"
t.integer "step_id"
t.string "phone"
end
add_index "text_messages", ["client_id"], name: "index_text_messages_on_client_id", using: :btree
create_table "users", force: true do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.inet "current_sign_in_ip"
t.inet "last_sign_in_ip"
t.string "first_name"
t.string "last_name"
t.datetime "created_at"
t.datetime "updated_at"
t.string "role"
t.string "title"
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "unconfirmed_email"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
end
text_message.rb
require 'twilio-ruby'
require 'date'
class TextMessage < ActiveRecord::Base
belongs_to :client, dependent: :destroy
belongs_to :step, dependent: :destroy
has_many :coach_emails
before_save :grab_phone
def grab_phone
self.phone = phone
end
def send_text_message(message, phone)
twilio_sid = ENV["TWILIO_ACCT_SID"]
twilio_token = ENV["TWILIO_AUTH_TOKEN"]
twilio_phone_number = ENV["TWILIO_PHONE_NUMBER"]
begin
#twilio_client = Twilio::REST::Client.new(twilio_sid, twilio_token)
#twilio_client.account.sms.messages.create(
:from => "+1#{twilio_phone_number}",
:to => phone,
:body => message)
rescue Twilio::REST::RequestError => e
puts e.message
end
if e != "400" || e != "500"
self.sentstatus = true
end
self.save!
end
end
routes.rb
Rails.application.routes.draw do
require 'sidekiq/web'
devise_for :users, :path => '',
:path_names => {:sign_in => 'login', :sign_out => 'logout'},
:controllers => { registrations: 'registrations' }
authenticate :user do
mount Sidekiq::Web => '/sidekiq'
end
resources :clients do
resources :action_plans, shallow: true, except: [:index]
end
resources :action_plans, shallow: true, only: [] do
resources :goals, shallow: true, except: [:index]
end
resources :goals, shallow: true, only: [] do
resources :steps, shallow: true, except: [:index, :destroy]
end
resources :steps, shallow: true, only: [] do
resources :text_messages, shallow: true, except: [:index, :destroy]
end
get "text_messages/receive"
match '/receivetext' => 'text_messages#receive', :via => :post
resources :clients do
collection do
post :welcome
end
end
get 'about' => 'welcome#about'
root to: 'welcome#index'
end
I figured out that I need to add to the TextMessage controller (#groupnew and #groupcreate).
New Routes
get 'group_new' => 'text_messages#group_new'
post 'group_create' => 'text_messages#group_create'
Updated TextMessage Controller
def group_new
#clients = current_user.clients.order(:last_name)
#text_message = TextMessage.new
end
def group_create
client_hash = params[:client_id]
client_hash.each do |client|
#client = Client.find(client)
content = params[:text_message][:content]
#text_message = #client.text_messages.build(text_message_params)
#text_message.incoming_message = false
#text_message.sentstatus = false
#text_message.phone = #client.phone
if #text_message.scheduled_date == nil
#text_message.scheduled_date = Date.today
# print time in Pacific time
#text_message.scheduled_time = Time.now.in_time_zone("Pacific Time (US & Canada)")
#text_message.send_text_message(#text_message.content, #text_message.phone)
else
#text_message.save
end
end
redirect_to clients_path
end
Group text now works :)

Active_admin and :filter

The issue lies in the following: filter :contact, :as => :string works successfully if I type the id for the contact. But that's not practical when you have 2000+ contacts. How can I successfully filter :contact, as => :string but have it search for :name instead of :id.
I have tried the following with no success:
filter :contact, :as => :string, :collection => proc {Contact.where(:name => 'Paul' )}
filter :contact, :as => :string, :collection => proc { (Contact.order.all).resources{|c| [c.name]}}
Note: my repository can be found here.
Model: order.rb
belongs_to :contact
Migration:
def change
create_table :orders do |t|
t.string :tag
t.text :description
t.string :technician_id
t.string :status
t.string :type
t.string :contact_id
t.string :business_id
t.timestamps
end
end
admin/orders/ - orders.rb
filter :business
filter :contact, :as => :string, :collection => proc { (Contact.order.all).resources{|c| [c.name]}}
filter :tag
filter :description, :label => "Status"
filter :created_at
index do
column :business
column :contact
column :tag
column :status
column :description, :sortable => false do |order|
truncate(order.description, :length => 30)
end
Activeadmin uses meta_search gem, so try this:
filter :contact_name, :as => :string

Resources