activerecord

Can a single record from a polymorphic model belong to two (or more) models at the same time?

六眼飞鱼酱① 提交于 2019-12-10 10:46:18
问题 General newbie question: If I have a polymorphic model called Message , and two other models called Filter and User with has_many: messages, as ... association on both. Can a single record from Message belong to User and Filter models at the same time? For example, can I do: ... User.find(1).messages << Message.find(1) Filter.find(1).messages << Message.find(1) ... and have Message#1 available in in User#1 and Filter#1 ? The RailsGuides gives a very brief explanation, so this aspect is still

Rails Search Query Associated Model

天大地大妈咪最大 提交于 2019-12-10 10:43:49
问题 In railscast #37 they show a simple search I am trying to implement. I have the following association: class Owner < ActiveRecord::Base has_many :dogs end class Dog < ActiveRecord::Base belongs_to :owner end The Dog model has an attribute "name" and so does the owner. I need to be able to filter a list of dogs so that if you type "Fido" (dog) or "Bob" (owner) Fido will show up in the list. Here is how the search is currently set up: model dog.rb: def self.search(search) if search find(:all,

ActiveRecord (CDbCriteria) vs QueryBuilder?

混江龙づ霸主 提交于 2019-12-10 10:24:14
问题 I have to make some filters, such as get persons who are in a given department, and I was wondering about the best way to do it. Some of them are going to require the join of multiple tables. Does anyone know about the main differences between CDbCriteria and Query Builder? I would particularly like to know about the compatibility with databases. I found this in the Yii documentation about Query Builder: It offers certain degree of DB abstraction, which simplifies migration to different DB

Adding an attribute to yii2 active record model that is not in database

我的未来我决定 提交于 2019-12-10 10:16:20
问题 I have a mySQL database that has a table videos and two columns, start_time and end_time , which are in the format 2017-01-24 15:38:11 . I have an active record model Videos that extends \yii\db\ActiveRecord and i would like to add a few additional attribute that are not in the database. I am trying to split the time stamp into a separate date and time that i can then display as columns in my gridview widget. I successfully did this by setting the column values directly in the view to

Comparing .references requirement on includes vs. eager_load

白昼怎懂夜的黑 提交于 2019-12-10 10:09:52
问题 I know that when you utilize includes and you specify a where clause on the joined table, you should use .references example: # will error out or throw deprecation warning in logs users = User.includes(:orders).where("Orders.cost < ?", 20) In rails 4 or later, you will get an error like the following: Mysql2::Error: Unknown column 'Orders.cost' in 'where clause': SELECT customers.* FROM customers WHERE (Orders.cost < 100) Or you will get a deprecation warning: DEPRECATION WARNING: It looks

Do ActiveRecord::Calculations with eager loading make multiple database queries?

心已入冬 提交于 2019-12-10 10:02:32
问题 My confusion stems from this question, where OP has a model like class Quote < ActiveRecord::Base has_many :items def calc_price sum = 0 #logic for summation end end In the answers, a couple of people have suggested using the sum method directly for calculating the sum of attributes def total_price items.sum('price') end If I eager load data using Quote.includes(:items).find(:all) , does the sum happen at the database's end, or does it use the objects already loaded in memory? If it uses the

Rails “where” method to find parent by child attribute

一个人想着一个人 提交于 2019-12-10 09:27:43
问题 I have a Rails app where I'm trying to create a list of parent classes based on the date of their child classes. right now I have: orders = Order.where("order_reminders.date < ?", 1.month.from_now) But I'm receiving an error. "no such column: order_reminders.date" I'm sure my problem is with my query but I'm not sure how to fix it. 回答1: You need to use methods like references , joins , includes or eager_load depending on what you need. The simplest way to determine what to select is to go to

Rails: has_many :through or has_many_and_belongs_to?

女生的网名这么多〃 提交于 2019-12-10 09:07:42
问题 I have an app where I want to link an instance of a model to another instance of the same model via another model (i.e. Task1 > Relationship < Task2) and am wondering if I can use has_many :through for this. Basically, the relationship model would have extra information (type_of_relationship, lag) so it would be ideal to have it as a joining model. However, there are not two models to join, only one … to itself. Would the has_many :through still work? If so, how would the join table look?

Ambiguous column in MySQL/Rails find method

自古美人都是妖i 提交于 2019-12-10 08:43:59
问题 I'm getting this error Mysql::Error: Column 'id' in field list is ambiguous when using a find method like such: self.prompts.find(:all, :select => 'id') The models are being called using a has_many :through association, so MySQL complains that there are multiple 'id' columns, since all 3 tables being used have an 'id' column. I looked this up and understand what is going wrong on the SQL end, but don't know how to resolve it in the ActiveRecord find method, and I'm not confident in my SQL

Using Active Record Reputation System gem, no sorting happens when I sort by votes

允我心安 提交于 2019-12-10 07:45:09
问题 Following the RailsCast for the reputation system gem, I added the following code to my microposts_controller def index @microposts = Micropost.paginate(page: params[:page]).find_with_reputation(:votes, :all, order: "votes desc") @micropost = current_user.microposts.build end But no sorting happens in my index action aside from the default scope I set in my model In my micropost model I have class Micropost < ActiveRecord::Base belongs_to :user has_many :retweets has_reputation :votes, source