activerecord

Rails ActiveRecord::Relation error

半腔热情 提交于 2019-12-23 05:11:59
问题 Looking into passing a variable as value on ActiveRcord query key/column? login = Login.where("email_address" => "geo@bostj.com","active" => 1).select("id") => [#<Login id: 767>] login = Login.where("email_address" => "geo@bostj.com","active" => 1).select("id").class => ActiveRecord::Relation admin = Admin.where("login_id"=>login).exists? Heeeelp 回答1: I would do: login = Login.where(email_address: 'geo@bostj.com', active: 1) Admin.exists?(login_id: login.pluck(:id)) 回答2: You can write as

rails models - two tables have the same primary and foreign key fields

℡╲_俬逩灬. 提交于 2019-12-23 05:02:12
问题 I am using an existing database with a rails app. I can't change the table or column names. Lets say table one is "invoices" and table 2 is "orders" they both have a primary key that is called the same thing, lets say "order_id" invoices can find its order by looking at the primary key "order_id" in the orders table. Vice versa for an order finding its invoice. Invoices can have but not always have one order (you might be invoiced for something besides an order, like a "work_order" which

Validate uniqueness of name in a HABTM association

爷,独闯天下 提交于 2019-12-23 04:49:12
问题 I have an Artist model that has many Album s in a HABTM association. Though I would like to permit two different albums to have the same name I'd like to ensure that there aren't two in the collection of one artist. So far example: artist_1 = Artist.create(name: "Jay-Z") artist_2 = Artist.create(name: "The Beatles") album_1 = Album.create(name: "The Black Album", artist_ids: [1]) album_2 = Album.create(name: "The Black Album", artist_ids: [1]) => Should return error album_2 = Album.create

Rails returning all of second level association

本秂侑毒 提交于 2019-12-23 04:43:04
问题 I currently have three models that I am interested in: Region, Seat and User. Region has many seats and seats have many available users. I'd like to know what is the most efficient way in Rails to retrieve all of the available users for the seats which sit under a particular region. Thanks in advance. 回答1: Try: User.joins(seat: :region).where(regions: { id: REGION_ID_HERE }) 回答2: I think the nested join syntax by @cschroed might not work depending on how your data model is set up. If it doesn

Multiple has_many relationships to the same class

感情迁移 提交于 2019-12-23 04:24:29
问题 I'm building a football game and I'm having trouble creating Club and Match classes. I want to be able to do this: match = Match.find(2) match.home_club = <some club here> match.away_club = <other club here> And also this: club = Club.find(2) club.matches # Returns all matches where club plays home or away This is what I have now: class Club < ActiveRecord::Base has_many :matches end class Match < ActiveRecord::Base belongs_to :home_club, :class_name => "Club" belongs_to :away_club, :class

Rails Dynamically Changing Collection Select In Form Based on form field

喜夏-厌秋 提交于 2019-12-23 04:23:06
问题 I have a Rails 3.2.14 app that tracks calls and each call has a pickup and dropoff facility from the Facility model which is an association. In the call model there is an association for the Region model (i.e. Houston, Dallas, Austin, etc) where we select a region based on where the call is coming from. What I'd like to do is be able to select a specific region (i.e. Houston) and in the pickup facility collection only show Houston region facilities. I'm assuming to start off with I'd need to

rails activerecord, friend relation + inverse_friend relation how to get the mutual relation? code included

不想你离开。 提交于 2019-12-23 04:11:20
问题 Trying to find the mutual relation, In a friends relations, Already have friends and inverse_friends. But how to combine them to get the mutual friends? Cannot seem to figure it out I tried several options and searched long time online, just don't see it has_many :friendships has_many :friends, :through => :friendships has_many :inverse_friendships, :class_name => "Friendship", :foreign_key => "friend_id" has_many :inverse_friends, :through => :inverse_friendships, :source => :user how to get

ActiveRecord querying a tree structure efficiently

大城市里の小女人 提交于 2019-12-23 04:04:30
问题 I have inherited a Rails 3 app that stores much of it's data as a fairly sophisticated tree structure. The application works pretty well in general but we are noticing some problems with performance, mostly around database interactions. I am seeing a lot of queries along the lines of these showing up in my logs: SELECT `messages`.* FROM `messages` WHERE `messages`.`context_type` = 'Node' AND `messages`.`context_id` IN (153740, 153741, /* about a thousand records... */ 154837, 154838, 154839,

Rails has_many :though STI

喜夏-厌秋 提交于 2019-12-23 04:01:41
问题 Lets start with code that may go like this: class User < ActiveRecord::Base has_many :photos has_many :submitted_photos has_many :posted_photos has_many :handshakes, through: :photos end class Photo < ActiveRecord::Base belongs_to :user end class PostedPhoto < Photo has_many :handshakes, inverse_of: :poster, foreign_key: 'poster_id' end class SubmittedPhoto < Photo has_many :handshakes, inverse_of: :submitter, foreign_key: 'submitter_id' end class Handshake < ActiveRecord::Base belongs_to

rails + ActiveRecord: caching all registers of a model

℡╲_俬逩灬. 提交于 2019-12-23 03:51:55
问题 I've got a tiny model (let's call it "Node") that represents a tree-like structure. Each node contains only a name and a reference to its father: class Node < ActiveRecord::Base validates_presence_of :name, :parent_id end The table isn't very big - less than 100 elements. It's updated rarely - in the last 4 months 20 new elements were added, in one occasion, by the site admin. Yet it is used quite a lot on my application. Given its tree-like structure, on some occasions a request triggers