rails-activerecord

Why is an apostrophe appearing as ' in Ruby on Rails and is this a sign of a security issue?

牧云@^-^@ 提交于 2019-12-10 20:33:55
问题 I'm new to Ruby on Rails and I'm using a form_helper to create and update records. In the form below, I'm collecting information to save data for maps that users can create. The :name field is the name that user gives to a map. It's saved to a MySQL table into a field that is varchar(255). If I name a map "John's Map", it appears in both the database and in the view as John's Map How can I prevent this from happening, and is my code susceptible to SQL injection with this approach? I've seen

Get all records grouped by field from association and sorted by count in group

非 Y 不嫁゛ 提交于 2019-12-10 19:06:18
问题 I have 3 models: Post , Comment , User Post has many Comments Comment belongs to User User has field country_code I want to get all post comments grouped by country code AND sorted by amount of comments per country. This query: post.comments.joins(:user).group("users.country_code").order('count_all desc').count returns such kind of result: {"DE"=>67, "US"=>8, "RS"=>8, "IN"=>8, "ES"=>7, "BR"=>6, ... "UA"=>0 } What I need is a similar result where country codes are keys but values are arrays of

What is the difference with find_by() from the core and the one from the FinderMethods?

China☆狼群 提交于 2019-12-10 18:21:08
问题 Currently I'm working on a gem, which overrides ActiveRecords where . By working on that, I stumbled on two different find_by implementations. One is in the core and it uses some kind of cache, whereas the one from the FinderMethods module calls where directly. What is the difference between these two implementations? When is which used? 回答1: I think it's that way: When you use something like this: User.find_by(...) The ActiveRecord::Core#find_by is called, as the Core is included into Base

How do I migrate an ActiveRecord model attribute from json to jsonb?

↘锁芯ラ 提交于 2019-12-10 17:48:34
问题 What should the migration look like? I would like to take advantage of the jsonb array querying technique. 回答1: I would write the migration this way: def change reversible do |dir| dir.up { change_column :models, :attribute, 'jsonb USING CAST(attribute AS jsonb)' } dir.down { change_column :models, :attribute, 'json USING CAST(attribute AS json)' } end end I don't know how this compares performance-wise to other solutions, but I tested this on a table with 120,000 records, each record having

Rails 4: undefined method `primary_key_name'

白昼怎懂夜的黑 提交于 2019-12-10 16:35:35
问题 I'm receiving the following error using Rails 4.0.0.beta: NoMethodError: undefined method `primary_key_name' for #<ActiveRecord::Reflection::AssociationReflection I don't get the exception when using Rails 3.2.x. I'm using Ruby 1.9.3-p194 for both Rails 3.2.13 and Rails 4.0.0.beta. The problem stems from the following has_many statement: class Store < ActiveRecord::Base has_many :relationships has_many :customers, :through => :relationships, :source => :user, :conditions => { :relationships =

Optimize difficult query (possibly with squeel)

大城市里の小女人 提交于 2019-12-10 16:16:31
问题 There is such code(using PublicActivity gem & Squeel) def index @activities = Activity.limit(20).order { created_at.desc } @one = @activities.where{trackable_type == 'Post'}.includes(trackable: [:author, :project]) @two = @activities.where{trackable_type == 'Project'}.includes trackable: [:owner] @activities = @one + @two end But it creates 8 SQL requests: SELECT "activities".* FROM "activities" WHERE "activities"."trackable_type" = 'Post' ORDER BY "activities"."created_at" DESC LIMIT 20

Why does my ActiveRecord scope with `merge` return an array?

不羁的心 提交于 2019-12-10 15:35:48
问题 I have a scope on my Contract model that uses merge and returns an array, not an ActiveRecord::Relation as I would like. Yes, I've seen it said that "It is an ActiveRecord::Relation, but Rails is intentionally lying to you". But in this case: The scope uses merge it only works if it's the last scope in the chain The object it returns says it's of class Array The object it returns has nothing about ActiveRecord in its ancestors Calling ActiveRecord::Relation methods like scoped on the return

where does database.yml get loaded in ActiveRecord in Rails 4?

狂风中的少年 提交于 2019-12-10 15:25:36
问题 What line (or method) in the ActiveRecord codebase does the config/database.yml for a Rails application get loaded? (I'm looking at 4.0.5 specifically, but if anyone has any information on >=4.0.5, that would be illuminating)? 回答1: It's inside the Railties, specifically in the file railties/lib/rails/application/configuration.rb in lines 101–116 (for Rails 4.0.5): https://github.com/rails/rails/blob/v4.0.5/railties/lib/rails/application/configuration.rb#L101-L116 # Loads and returns the

Using ActiveRecord to achieve complex relations in Rails

不问归期 提交于 2019-12-10 13:39:44
问题 From another question of mine: What I need to achieve is this: Create multiple categories with their own designated information fields (ie. Cars have different fields from Pets) and once such a category is created, the commands I need will be invoked and a new table for each category will be made. I know I could store all fields as some sort of string and then process it to display it properly, but I need a advanced search function for my web app and creating separate tables for each category

has_one through association with condition

ε祈祈猫儿з 提交于 2019-12-10 13:20:55
问题 I have 3 relevant tables/models. I would like to retrieve the user which has organized a party , given a Party record, a join table with a boolean organized column, and a users table. My best attempt so far (this one makes the most sense to me after much fiddling). I've omitted the irrelevant columns. class Party # party_id has_many :parties_users has_many :users, through: :parties_users, source: :user has_one :organizer, -> { where organizer: true }, through: :parties_users, source: :user