activerecord

Is there a way to ensure one object reference per record in an ActiveRecord hierarchy?

本秂侑毒 提交于 2019-12-11 00:44:34
问题 It seems when I grab some hierarchical ActiveRecord structure that there are quite a few hits to the database. I improved this using the :include option to flesh out as much of the structure as possible. Even so, it seems that ActiveRecord does not map the reciprocal sides of a relationship (e.g. parent-child) with unique references to a record. That is, we get a navigable ActiveRecord structure, but one that to my knowledge doesn't guarantee a unique copy of a given record. node = Node.find

How to sort by related table field when sending Yii2 REST GET request

两盒软妹~` 提交于 2019-12-11 00:35:35
问题 I want to expand this question. Basically I have users endpoint. But I am also returning data from the related profiles table. I am not expanding with profiles, I always want to return it. So I have fields method like this: public function fields() { $fields = parent::fields(); $fields[] = 'profile'; return $fields; } When I do GET request and demand sorting by profile.created_at field and user.status, it does not sort by profile.created_at. GET v1/users?sort=-profile.created_at,status Can

Rails Active Record Query for Double Nested Joins with a Select Call

混江龙づ霸主 提交于 2019-12-11 00:30:09
问题 I have the following schema: Photos has many Groups has many Users. I am using this Rails server as a backend to an iOS application and constantly need to push out notifications to all involved users of a group when a photo is added. I problem is finding the least expensive query to resolve only the User.ids affected. So far I have Photo.find(1).groups.joins(:users) I know that I have to put a select argument after this, but can't figure out the syntax for the life of me. Given a photo, I am

Better way to access individual Rails ActiveRecord error?

时光怂恿深爱的人放手 提交于 2019-12-11 00:16:01
问题 I'm trying to access the type attribute of an ActiveRecord::Error object. The reason I'm doing this is because I want to redirect a user to a different page depending on the type of validation that failed (an attribute can fail validation in several ways, so the attribute itself is insufficient). The only way I've found that I can do this is: obj.errors.instance_variable_get(:@errors)["attr"][0].type which is just plain nasty. Is there a better way? 回答1: Looks like your best bet is to extend

How to set up multiple aliased joins between two models?

自闭症网瘾萝莉.ら 提交于 2019-12-11 00:02:57
问题 In a Rails 3.2 app I need to set up two associations between the same two models. For example class User has_many :events has_many :attendances has_many :attended_events, through: :attendances end class Event belongs_to :event_organizer, class_name: "User" has_many :attendances has_many :attendees, through: :attendances, class_name: "User" end class Attendance belongs_to :attended_event, class_name: "Event" belongs_to :attendee, class_name: "User" end This is the first time I've experimented

CONCAT() function in a SELECT field list

二次信任 提交于 2019-12-11 00:00:53
问题 I'm using CodeIgniter's active record features but I'm not able to select the data I'm interested in. What I want to be selected is: CONCAT(t.field1, ' / ', t.field2) AS `finalValue` So I add this: $this->db->select('CONCAT(t.field1, \' / \', t.field2) AS `finalValue`'); But this is the query string that's generated: CONCAT(t.field1, `'` / ', `t`.`field2)` AS `finalValue` Is this a bug? Am I specifying it incorrectly? 回答1: You can actually turn off the default escaping mechanism, which is the

refactoring: swicthing from custom data access layer to Entity Framework

梦想的初衷 提交于 2019-12-10 23:54:48
问题 I am a .NET Developer and as part of a refactoring project, I have a few questions. Our software is currently using an Active Record pattern (one to one mapping between the data objects and the business objects). The bad thing is that the business objects inherits from the data objects, inducing highly couple between the layers. Our goal is to switch from our custom data access layer (based on ADO.NET) to Entity Framework. The constraint we have is not to break the code so that our older

Rails 3 ActiveRecord & Postgres: order by count on association

梦想的初衷 提交于 2019-12-10 23:54:30
问题 I have the following 2 rails models: class Profile < ActiveRecord::Base belongs_to :user has_many :votes, through: :user default_scope includes(:user) end and class Vote < ActiveRecord::Base attr_accessible :by, :for belongs_to :by, class_name: "User" belongs_to :for, class_name: "User" validates :by, :for, presence: true validates_uniqueness_of(:by, scope: :for) end I'm trying to create a "top" scope on Profile which sorts the Profiles based on the number of "for" votes the associated user

RAILS: How to query for all the objects whose every association have an attribute that is not null

断了今生、忘了曾经 提交于 2019-12-10 22:40:03
问题 Im working on Rails 3.0.5 and PostgreSQL. I have a model Offer , that has many Products . class Offer < ActiveRecord::Base has_many :products end class Product < ActiveRecord::Base belongs_to :offer end The product has an id that updates when you register it in a third party service. Lets call it service_id . I want an Offer scope, that gets all the offers that have every single product registered in the third party service. In other words, I'm only interested in the offer instance if all of

How can I order my entries by sum from a separate table?

浪子不回头ぞ 提交于 2019-12-10 22:25:47
问题 I am wondering how I can order posts in my PostController#index to display by a column total in a separate table. Here is how I have it set up. class Post < ActiveRecord::Base :has_many :votes end and Class Vote < ActiveRecord::Base :belongs_to :post end I user can either vote up or down a particular post. I know there are likely better ways to do what I am currently doing but looking for a fix given my current situation. When a user votes up a post, a value of 1 is passed to the Vote Table