activerecord

Joins across multiple tables with ActiveRecord with named scopes

孤者浪人 提交于 2020-01-14 05:12:06
问题 I love making named scopes for rails. however, I ran into somewhat of a pickle. Ive gotten pretty comfortable using named scopes for joins like so: named_scope :foo, :joins => :bar, :conditions => "bar_attribute = 'something'" Now pretend I have a table called baz which is contains a foreign key from the bar table. I need something like this: named_scope :foo, :joins => (:bar => :baz), :conditions => "bar.id = baz.bar_id AND baz_attribute = 'something_else'" How possible is this? thanks 回答1:

Comparing rspec custom ActiveRecord::RecordInvalid errors messages

蓝咒 提交于 2020-01-13 19:40:50
问题 Having the following in the model : validates_uniqueness_of :title, if: proc { |item| item.item_type == 'tag' }, case_sensitive: false, message: I18n.t('errors.key', value: "%{value}") and trying to validate in rspec with the following : expect { xxx }.to raise_error( ActiveRecord::RecordInvalid, I18n.t('errors.key', value: '...passing the title...') ) Am stuck with the very close following : ActiveRecord::RecordInvalid with "translated error", got #<ActiveRecord::RecordInvalid: Validation

Comparing rspec custom ActiveRecord::RecordInvalid errors messages

天涯浪子 提交于 2020-01-13 19:39:39
问题 Having the following in the model : validates_uniqueness_of :title, if: proc { |item| item.item_type == 'tag' }, case_sensitive: false, message: I18n.t('errors.key', value: "%{value}") and trying to validate in rspec with the following : expect { xxx }.to raise_error( ActiveRecord::RecordInvalid, I18n.t('errors.key', value: '...passing the title...') ) Am stuck with the very close following : ActiveRecord::RecordInvalid with "translated error", got #<ActiveRecord::RecordInvalid: Validation

Rails is this query open to sql injection?

青春壹個敷衍的年華 提交于 2020-01-13 13:11:18
问题 I'm still learning how to write good queries using ActiveRecord. I'm curious if this query is subject to sql injection because of the way i'm using the date field in the query. Can someone please point out any obvious mistakes or any better ways to write this query? @arrangements_for_month = Arrangement.joins(:timeslot). where("timeslots.timeslot BETWEEN '#{month}' AND '#{month.end_of_month}'", params[:id]). order('location_id') 回答1: You should just use the preferred way of including

How to increase ActiveRecord connection timeout?

耗尽温柔 提交于 2020-01-13 11:43:11
问题 Is there a way to increase the connection timeout for ActiveRecord? I keep getting this error when I have more than 25 threads with a pool size of 5. (ActiveRecord::ConnectionTimeoutError) "could not obtain a database connection within 5 seconds (waited 5.000144774 seconds). The max pool size is currently 3; consider increasing it." If there is not a way to increase the connection timeout, what is the best way to ensure a thread is using a connection as quick as possible? 回答1: You can add

How to increase ActiveRecord connection timeout?

安稳与你 提交于 2020-01-13 11:43:06
问题 Is there a way to increase the connection timeout for ActiveRecord? I keep getting this error when I have more than 25 threads with a pool size of 5. (ActiveRecord::ConnectionTimeoutError) "could not obtain a database connection within 5 seconds (waited 5.000144774 seconds). The max pool size is currently 3; consider increasing it." If there is not a way to increase the connection timeout, what is the best way to ensure a thread is using a connection as quick as possible? 回答1: You can add

Yii condition <IS NULL>

帅比萌擦擦* 提交于 2020-01-13 11:15:54
问题 How to find row by column with null value? It doesn't work: $criteria->condition = '`seller_id` IS NULL'; 回答1: $criteria->addCondition('seller_id IS NULL'); $data= MODEL::model()->find($criteria); tried this? **seller_id** is the mapped column name in Yii , it may not be same as the actual column name in your database. 来源: https://stackoverflow.com/questions/5349788/yii-condition-is-null

Yii condition <IS NULL>

社会主义新天地 提交于 2020-01-13 11:14:48
问题 How to find row by column with null value? It doesn't work: $criteria->condition = '`seller_id` IS NULL'; 回答1: $criteria->addCondition('seller_id IS NULL'); $data= MODEL::model()->find($criteria); tried this? **seller_id** is the mapped column name in Yii , it may not be same as the actual column name in your database. 来源: https://stackoverflow.com/questions/5349788/yii-condition-is-null

Yii condition <IS NULL>

邮差的信 提交于 2020-01-13 11:13:18
问题 How to find row by column with null value? It doesn't work: $criteria->condition = '`seller_id` IS NULL'; 回答1: $criteria->addCondition('seller_id IS NULL'); $data= MODEL::model()->find($criteria); tried this? **seller_id** is the mapped column name in Yii , it may not be same as the actual column name in your database. 来源: https://stackoverflow.com/questions/5349788/yii-condition-is-null

Rails Pagination without Gem

浪尽此生 提交于 2020-01-13 10:44:29
问题 I have been tasked to create 'previous 10', 'next 10', 'newest', and 'oldest' links for a Rails project WITHOUT using a gem. In the controller, I can show the first (newest) set of 10 items in the table: ... before_action :set_page, only: [:index] ... def index @rows = Row.order(created_at:).limit(10) end ... private def set_page @page = params[:page] || 0 end ... However, I don't believe this correctly sets the new pages with 10 each as I am unable to change the page number (hxxp://...?page