activerecord

ActiveRelation that requires nested joins using scopes

老子叫甜甜 提交于 2020-01-16 04:34:07
问题 I am new to rails. Having a blast. The query API though is giving me some trouble. I've been zooming and doing a lot of stuff very quickly, but this is the first time I have spent hours trying to figure it out. It's not like anything I've used before - regular SQL, or Hibernate, or whatever. The model I have is pretty simple. A PrivateMessage has many Recipients A Recipient has a Receiver (which of class User) recipient also has fields for 'is_read' and 'is_deleted' My goal is to build a

Ruby on Rails query not working properly

孤街醉人 提交于 2020-01-16 04:09:07
问题 I have several listings, and I define a number of filters. In particular, a listing has_many :spaces, through: :designations and has_many :amenities, through: :offerings . I use the filters to restrict the listings that get shown. The two main ones are: # filter by space type if params[:search][:space_ids].present? && params[:search][:space_ids].reject(&:blank?).size > 0 @listings = @listings.joins(:spaces).where('space_id IN (?)', params[:search][:space_ids].reject(&:blank?)).uniq end #

Parentheses passed in url break codigniter query

陌路散爱 提交于 2020-01-16 03:56:08
问题 I have a string being passed via an URL to a Codeigniter driven site. The string is urlencoded before being passed, then urldecoded by the controller before being passed to a function in the model that searches for the string in a database table. I've read several other posts/articles on this, but none offer a viable (for me) solution to the issue. Here are some of the things I have read: URL having parentheses not working in codeigniter with datamapper ORM Parenthesis issue in codeigniter

Use where queries in JSONB datatype in Rails Postgres

家住魔仙堡 提交于 2020-01-15 18:50:48
问题 How to query out schedules who has slot_details which have empty appointment_id. Here is an example data set: id: 98, consulting_location_doctor_id: 498, schedule_date: Thu, 15 Jan 2015 00:00:00 IST +05:30, slot_details: [{"end"=>"2015-01-15T15:00:00.000+00:00", "start"=>"2015-01-15T14:30:00.000+00:00", "title"=>" 2:30 pm to 3:00 pm", appointment_id: ""}, {"end"=>"2015-01-15T15:30:00.000+00:00", "start"=>"2015-01-15T15:00:00.000+00:00", "title"=>" 3:00 pm to 3:30 pm", appointment_id: ""}, {

How to isolate id from string primary key in Heroku Postgres

对着背影说爱祢 提交于 2020-01-15 10:44:08
问题 I have a model where I'm using a string as a primary key: class Employee < ActiveRecord::Base self.primary_key = "employment_id" end This table also contains the Rails default 'id' field, with a uniqueness constraint. When I add a new employee locally it all works fine, with Rails automatically generating a new unique id. However when I run this on Heroku Postgres it appears to treat 'id' and 'employment_id' as the same field. I tried to get around this by manually setting a unique id, but

Search form not routing to proper controller in Rails 5.1

独自空忆成欢 提交于 2020-01-15 09:09:22
问题 I've been struggling for few days on this one: I have a search form in my navbar that runs a search in the :address column of my Users table through the job model. Here is the index method of my JobsController: def index @jobs = if params[:address] Job.joins(:user).where(users: { address: params[:address].downcase }).paginate(page: params[:page], per_page: 3) else @jobs = Job.paginate(page: params[:page], per_page: 3).order('id DESC') end end Here is the form: <form class="navbar-form navbar

Why does this :id in Rails not work with Postgresql but it does work with MySQL?

独自空忆成欢 提交于 2020-01-15 08:42:16
问题 I am using this method for search engine friendly URLs in Rails - http://jroller.com/obie/entry/seo_optimization_of_urls_in My model looks like this. class Listing < ActiveRecord::Base def to_param "#{id}-#{title.parameterize}" end end This works with MySQL but not Postgresql. This is the error I get: ActiveRecord::StatementInvalid (PGError: ERROR: invalid input syntax for integer: "16- college-station-apartments" : SELECT * FROM "floorplans" WHERE ("floorplans"."listing_id" = E'16-college

Rails foreign key column name convention for a unary many-many relation

廉价感情. 提交于 2020-01-15 08:28:26
问题 I know that if there is a "users" table and a "posts" table, the posts table has a foreign key column named 'user_id', according to the rails convention. My problem arises when I have a "users" table and the relation that i am trying to make is unary many-many (many buyers can notify many sellers, buyers and sellers are both "users"). Now "notifications" is the join table, that must contain the foreign keys "user_id" and "user_id" for storing the buyer and seller ids according to the rails

Rails foreign key column name convention for a unary many-many relation

瘦欲@ 提交于 2020-01-15 08:28:09
问题 I know that if there is a "users" table and a "posts" table, the posts table has a foreign key column named 'user_id', according to the rails convention. My problem arises when I have a "users" table and the relation that i am trying to make is unary many-many (many buyers can notify many sellers, buyers and sellers are both "users"). Now "notifications" is the join table, that must contain the foreign keys "user_id" and "user_id" for storing the buyer and seller ids according to the rails

Active record has_many association for one model having two types of values

那年仲夏 提交于 2020-01-15 08:17:34
问题 i am facing a new situation. I know someone definitely had faced these type of situation: I have an invoices table and an invoice_line_items table. Till now every thing is going well with has_many and belongs to association. Now i want to add tax accounts in invoice items, for that i don't want to create a separate table, hence i make below changes in my invoice model : invoice.rb: class Invoice < ActiveRecord::Base has_many :invoice_line_items has_many :tax_line_items, :class_name =>