activerecord

rails polymorphic model implementation

拜拜、爱过 提交于 2020-01-06 11:25:54
问题 In my project, I have a relationship model that allow users to follow each other. class Relationship < ActiveRecord::Base attr_accessible :followed_id belongs_to :follower, :class_name => "User" belongs_to :followed, :class_name => "User" end Now, I want to also allow users to follow courses and groups. Do I start a new followedCourse and followedGroup model or do I make the relationship model polymorphic? How do I do that? Thanks. 回答1: I wouldn't use polymorphic for potentially-large tables.

Need to avoid SQL Injection in Rails3

荒凉一梦 提交于 2020-01-06 08:44:49
问题 I need the following code to be rewritten by avoiding SQL Injection in Rails 3. some_table_name.joins("inner join #{table_name} on linked_config_items.linked_type = '#{class_name}' and linked_config_items.linked_id = #{table_name}.id"). where("#{table_name}.saved is true and #{table_name}.deleted_at is null") Here, table_name is dynamic and it will vary. 回答1: SQL identifiers like table names and column names cannot be replaced with bound parameters, which is the more common method to ensure

Rails :include doesn't include

邮差的信 提交于 2020-01-06 08:18:36
问题 My models: class Contact < ActiveRecord::Base has_many :addresses has_many :emails has_many :websites accepts_nested_attributes_for :addresses, :emails, :websites attr_accessible :prefix, :first_name, :middle_name, :last_name, :suffix, :nickname, :organization, :job_title, :department, :birthday, :emails_attributes end class Email < ActiveRecord::Base belongs_to :contact validates_presence_of :account validates_format_of :account, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :on =>

update_attributes is not working

不羁岁月 提交于 2020-01-06 06:59:26
问题 Here's my controller class ActivitiesController < ApplicationController def exercises if current_user.userprofile.present? #chef whether there is a userprofile object @weeknum = current_user.userprofile.week @dayly_activity = Activity.where(:week => 1, :day => 'Monday').first end #end check userprofile end def updatexercises respond_to do | format | @dayly_activity = Activity.where(:week => 1, :day => 'Monday').first @dayly_activity.update_attributes(params[:@dayly_activity]) @dayly_activity

Ruby on rails active-record generated SQL on Postgres

偶尔善良 提交于 2020-01-06 06:07:53
问题 Why does Ruby on rails generated more queries in the background on Postgres than MySQL? I haven't tried deploying Rails on production with Postgres yet, but I am just afraid this generated queries would affect the performance. Do you find Rails with Postgres is slower than MySQL, knowing that it produce more query on the background? Or it is relatively the same? 回答1: i don't think that Postgres will affect your performance, despite it uses more resource, since its a full RDBM implementation,

What's the difference between “left_joins” and “includes” in Rails 5?

僤鯓⒐⒋嵵緔 提交于 2020-01-06 05:35:06
问题 In Rails 5, they have added the ActiveRecord query method left_joins . What's the difference between left_joins and includes ? I've always seen includes as a left join. 回答1: includes by default loads the association data in 2 queries just like preload. But with additional references call it switches from using two separate queries to creating a single LEFT OUTER JOIN like left_joins. Refs: Preload, Eagerload, Includes and Joins Making sense of ActiveRecord joins, includes, preload, and eager

How to check if a number is within range in activerecord?

China☆狼群 提交于 2020-01-06 05:24:06
问题 I have a model with a range column #<JobRequirement id: 1, age: 18...30> How can I get the JobRequirement using an age: 20 ? Something like JobRequirement.where(age: 20) 回答1: I think you need to use the PostgreSQL range operators and a bit of SQL in a string. In particular, you'd want the @> (contains element) operator: JobRequirement.where('age @> ?', 20) As far as supplying a range as a placeholder value goes: JobRequirement.where('age <@ ?', 18..20) you'll find that AR's knowledge of

Rails ActiveRecord find children where attribute is NOT a given value

回眸只為那壹抹淺笑 提交于 2020-01-06 05:09:05
问题 Given that a Parent has many Child s with status_id attribute, I want to find all the children that do NOT have a status_id:1 . In other words, the status_id could be nil or a different value. But I'm seeing some interesting behavior: Parent.find(1).childs.where(status_id:nil) => #<ActiveRecord::AssociationRelation [#<Child id: 1, status_id: nil ...>] Parent.find(1).childs.where.not(status_id:1) => #<ActiveRecord::AssociationRelation []> 回答1: This post suggests that SQL treats NULL, the

Database query for dynamic search with ActiveRecord or pure SQL ActiveRecord::AssociationRelation

六月ゝ 毕业季﹏ 提交于 2020-01-06 04:52:15
问题 In my db I don't have column company_name but I have records that inquiry_fields: { name: 'company_name' } and under their value I will get string with company name. To get one company name I have to do: first_process = Process.first .inquiry_field_responses .joins(:inquiry_field) .where(inquiry_fields: { name: 'company_name' }) which returns ActiveRecord::AssociationRelation so I have to do: first_process.first&.value And I will get string with company name InquiryFieldResponse Load (0.8ms)