activerecord

Ruby On Rails multiple composite primary keys question

久未见 提交于 2019-12-08 04:42:59
问题 I am a new guy in Ruby, and I have tables with these primary keys: transaction_types: transaction_type transaction_headers: transaction_type transaction_year transaction_id transaction_details: transaction_type transaction_year transaction_id city_id ticker_id tickers: city_id ticker_id Of course, those models have other non primary keys such as customer_id, connection_id or date, or user_id, etc, but those are not important for relationships, as those are merely data or I don't have any

Rails 5.1.: destroy records in “has_many: through” association with restriction

冷暖自知 提交于 2019-12-08 04:41:01
问题 I have classic has_many: through relationship: class UserGroup < ApplicationRecord has_many :user_groups_users has_many :users, through: :user_groups_users end class UserGroupsUser < ApplicationRecord belongs_to :user_group belongs_to :user end class User < ApplicationRecord has_many :user_groups_users has_many :user_groups, through: :user_groups_users end and in order to destroy UserGroup record, I need to destroy appropriate records in UserGroupsUser , which both is part of a gem. Otherwise

Where has create/create_record gone in ActiveRecord?

随声附和 提交于 2019-12-08 04:26:16
问题 I have an audit monkey patch to ActiveRecord that works on all versions of Rails from 2.x to 4.0.2 but does not work with ActiveRecord 4.1. The code for 4.0 looks like this module HLLAuditStamps def self.included(base) # create/update became create_record/update_record in Rails-4.0 base.alias_method_chain :create_record, :audit base.alias_method_chain :update_record, :audit private def create_record_with_audit set_audit_attributes . . . This works in 4.0.2 but throws this exception in 4.1.2:

Why can't I directly access instance variables in Active Record callback?

落爺英雄遲暮 提交于 2019-12-08 04:26:11
问题 In Active Record callbacks, I always see examples using self.variable . Sometimes, the self. prefix isn't needed, so they use just variable (see this related question). From what I've read, these two methods of referencing the class instance variable are using the accessor function, whereas @variable would directly access the variable. In my code, I tried to use @variable and it didn't work - it was as if @variable was not defined yet... can you not access instance variables directly in an

ActiveRecord touch causing deadlocks

こ雲淡風輕ζ 提交于 2019-12-08 04:16:14
问题 My app uses touch extensively in order to take advantage of Rails' template caching system. There's a certain type of work my app does when many relationships are created between many different objects in a batch. Sometimes, some of this work results in the resulting cascading touch es causing deadlock. I can code around this for the one scenario where I am seeing it happen often, but seeing it has brought to light the larger problem, which could happen in other scenarios, albeit it's very

how can I use like query in ruby with sinatra?

依然范特西╮ 提交于 2019-12-08 04:11:32
问题 Here's the query I tried: @blogs = DB[:blogs].where(:title => params[:s_txt]).reverse_order(:id) In this query, I'd like to find blogs in my database. I also need to create a query that gives users more results. How can I do this? 回答1: I also had this problem and only found this solution @blogs = DB[:blogs].where("title LIKE '%#{params[:s_txt]}%'").reverse_order(:id) Curious if there are better ways.. Searching in two fields is repeating the LIKE and seperating by AND or OR @blogs = DB[:blogs

Many-to-Many relationship only works one way Rails / Activerecord / devise

旧巷老猫 提交于 2019-12-08 04:05:35
问题 I have a many-to-many relationship User to Lists User model was created with devise. For some reason I cant access .lists on any of the users. returns undefined method `to_sym' for nil:NilClass. after doing some digging im pretty sure ive found the problem in _reflect_on_associationactiverecord (4.1.6) lib/active_record/reflection.rb def _reflect_on_association(association) #:nodoc: _reflections[association.to_sym] end association is being passed in as nil. Has anyone run into this problem

has_many :through usage, simple, beginner question

[亡魂溺海] 提交于 2019-12-08 04:03:50
问题 I am writing a simple accounting system for managing costs. The structure is like this: Invoice - can have many products Product - can have many costs, also can act_as_tree LineItem - Product LineItem LineItem Product LineItem Product LineItem LineItem I had this set up as a has_many and belongs_to for the three classes but think that the following is more appropriate (based upon reading Rails 3 Way - any shortcomings are my lack of understanding; just trying to give context) Class Invoice <

Rails4 : Model caching (low level caching)

别来无恙 提交于 2019-12-08 04:02:05
问题 I am trying to cache results of Active Record Query. Here is my use case: User.rb def self.awesome_users_cached Rails.cache.fetch("awesome_users") { where(awesome: true) } end UsersController def index all_awesome_users = User.awesome_users_cached less_awesome_users = User.where(less_awesome_user:true).pluck(:id) @users = all_awesome_users.where.not(id: less_awesome_users) end Here are 2 issues: self.awesome_users_cached doesnt cache the results and ends up hitting the all_awesome_users ends

Transaction doesn't work in Yii

99封情书 提交于 2019-12-08 03:31:15
问题 this is my code: $transaction = Yii::app()->db->beginTransaction(); try { $tModel->save(); $activationLink = new ActivationLink; $activationLink->User_id = $tModel->id; $activationLink->hash1 = User::generateHashCode(100); $activationLink->hash2 = User::generateHashCode(); $activationLink->hash3 = User::generateHashCode(); $activationLink->time = time(); $activationLink->save(); User::sendActivatonLink($tModel->mail,$activationLink->id, $activationLink->hash1, $activationLink->hash2,