activerecord

Maximum length of an SQL Query

自作多情 提交于 2020-01-03 07:18:34
问题 SELECT f.* FROM feeds f, user_feeds uf WHERE (f.id=uf.feed_id and uf.user_id in (1,2,5,6,23,45)) ORDER BY created_at DESC This is a query used to construct a user's feeds. The issue I have with this query is that the "uf.user_id in ()" increases as the number of users he follows increase. What is the allowed max length of an SQL query ? Is there a better way to implement my above query ? Note: I am using ActiveRecord. And I am using Postgres. 回答1: The maximum length of a query that PostgreSQL

has_and_belongs_to_many relationship not associating both ways

感情迁移 提交于 2020-01-03 05:50:11
问题 i setup a relationship using has_and_belongs_to_many to associate users and events. Then I try this: user = User.find(1) event = Event.find(1 ) both of these are not currently associated...then I try to associate them by doing: user.events << event this action works...however, they don't associate correctly for each other: user.events lists the event correctly for this user...but event.users does not have that user associated with it. how do I make it so that when I associate one with the

How do you do this in mysql or rails

若如初见. 提交于 2020-01-03 05:50:08
问题 Say you have a posts table and a tags table, and both are related by a post_tags table. so posts has id/subject/body columns tags has id/name post_tags has id/post_id/tag_id in rails terminology, I have a Post Model that has many Tags through AssetTags. I'm trying to query for a post that has 2 specific tags. so if there is a rails tag and a mysql tag, I want a query that returns a post that only has those two tags. make sense? Any way to do this with activerecord (I'm using search logic) or

N+1 in rails relations with active records?

耗尽温柔 提交于 2020-01-03 05:41:07
问题 I have four models Group Reports Comments user Group => has_many => Reports Report => has_many => Comments Comment => Belongs_to => User When i want to show a group I do something like <%= @group.name %> <%= @group.reports.includes(:comments).each do |report| %> <%= report.name %> <% report.comments.each do |comment| %> <%= comment.name %> <%= comment.user.name %> <% end %> <% end %> What is the best way to solve N+1 Query problems in this case ?? 回答1: Perhaps @group.reports.includes(

My model has a list of models. How do I load it from the database but limit the number of items in the list?

℡╲_俬逩灬. 提交于 2020-01-03 05:34:08
问题 In my Rails project I have a scenario where a car can have many parking tickets (I am using ActiveRecord). This is how I modelled it class Car < ActiveRecord::Base has_many :parkingTickets, :order => "partkingTickets.date DESC" end class ParkingTickets < ActiveRecord::Base belongs_to :Car end And this is how I retrieve data: @cars = Car.includes(:parkingTickets).order('ID, parkingTickets.date desc') This works. But now I want to get the car only with the tickets from the last 12 months (if

How to make a query in Postgres to group records by month they were created? (:created_at datetime column)

▼魔方 西西 提交于 2020-01-03 05:25:06
问题 I have this Ruby code: def visits_chart_data(domain) last_12_months.collect do |month| { created_at: month, count: domain.visits.created_on_month(month).count } end end def last_12_months (0..11).to_a.reverse.collect{ |month_offset| month_offset.months.ago.beginning_of_month } end CreatedOnMonth is just a scope: scope :created_on_month, -> (month = Time.now) { where{ (created_at >= month.beginning_of_month) & (created_at <= month.end_of_month) } } created_at is just a standard datetime

Sanitizing SQL in Rails where conditions may be NULL

前提是你 提交于 2020-01-03 04:30:06
问题 I'm struggling to sanitize a raw SQL query in which the WHERE conditions may either have a value or be NULL. I was hoping to use Active Record's built-in sanitizers... ( NOTE: I'll be using a simplified query for demo purposes- our real one is a complex UNION across different model types that would be hard to do with the AR query interface) Try 1: raw_query = "SELECT * FROM folders WHERE user_id = ? AND parent_id = ?" sanitized_query = ActiveRecord::Base.send(:sanitize_sql_array, [raw_query,

map(&:id) work but not pluck(:id)

做~自己de王妃 提交于 2020-01-03 04:00:13
问题 In part of my code I need to grab users's id but pluck doesn't grab them. Only map(&:id) can do it. I was wondering why. I've wrote a quick and dirty block of code to find what's happen def remove_users user_ids p users_to_remove.class users_to_remove = self.users.where(id: user_ids) if users_to_remove.present? self.users -= users_to_remove self.save p users_to_remove.class p users_to_remove Rails::logger.info "\n#{users_to_remove}\n" users_to_remove_map = users_to_remove.map(&:id) p users_to

How can I generate a percentage for a regex string match in Ruby?

旧时模样 提交于 2020-01-03 03:48:08
问题 I'm trying to build a simple method to look at about 100 entries in a database for a last name and pull out all the ones that match above a specific percentage of letters. My current approach is: Pull all 100 entries from the database into an array Iterate through them while performing the following action Split the last name into an array of letters Subtract that array from another array that contains the letters for the name I am trying to match which leaves only the letters that weren't

Yii2 Activerecord get fields from junction table and order by according to them

心已入冬 提交于 2020-01-03 03:31:09
问题 I have items and units table that have many to many relationship. In other words, the item has many units and the unit has many items. I managed the relation through a junction table item_units . The junction table has some extra field more than item_id and unit_id , i.e it has price, and weight (it is an integer to manage the order of units for each item for display purposes). I managed the relations in the models as follows: //In Items model /** * @return \yii\db\ActiveQuery */ public