activerecord

Sort list of objects according to an array (Rails)

ぐ巨炮叔叔 提交于 2019-12-08 01:28:25
问题 I have an array if ids from a table e.g: b = [659, 658, 656, 645, 644, 657, 634, 643, 649, 650, 651, 636, 633, 607, 605, 604, 648, 647, 675, 674, 667, 499] I use it for retrieving a list of objects from tat table: k = Photo.where(id:b) What I get is a list of objects which are NOT sorted in the same order as the array. How can I do that? 回答1: If ids are unique You can use index_by and values_at : k = Photo.where(id: b).index_by(&:id).values_at(*b) Example : b = [5,3,1] Country.where(id: b) #=

MySQL: remove consecutive duplicate values

夙愿已清 提交于 2019-12-08 01:19:57
问题 I have a MySQL table that returns a list of values that contains consecutive duplicates (when ordered by a timestamp). For example, when querying, I need to only return the consecutively duplicated values: [1, "Yellow"] [2, "Yellow"] [3, "Green"] [5, "Black"] [6, "Green"] [7, "Green"] The numbers here are being used for reference - the value is actually the string "Green", so for the above case the new unduped list would be: [1, "Yellow"] [3, "Green"] [5, "Black"] [6, "Green"] Is there a

Subsonic 3 ActiveRecord nested select for NotIn bug?

偶尔善良 提交于 2019-12-08 01:01:27
问题 I have the following Subsonic 3.0 query, which contains a nested NotIn query: public List<Order> GetRandomOrdersForNoReason(int shopId, int typeId) { // build query var q = new SubSonic.Query.Select().Top("1") .From("Order") .Where("ShopId") .IsEqualTo(shopId) .And(OrderTable.CustomerId).NotIn( new Subsonic.Query.Select("CustomerId") .From("Customer") .Where("TypeId") .IsNotEqualTo(typeId)) .OrderDesc("NewId()"); // Output query Debug.WriteLine(q.ToString()); // returned typed list return q

Can't create a new Spree app: “undefined method `raise_in_transactional_callbacks='”

て烟熏妆下的殇ゞ 提交于 2019-12-08 00:46:38
问题 I had this issue a couple of months ago, posted it on Spree's GitHub page, then figured out the solution myself and posted it on that same issue. Two months later, I'm still getting comments on that same issue from Googlers who had the same error message, so I figure it's worth posting my solution on this website as well just in case anyone is having the same problem but isn't looking on GitHub. The issue: trying to create a brand new Spree app, following the exact instructions in Spree's

how to insert into multiple tables in rails

廉价感情. 提交于 2019-12-08 00:44:57
问题 i am trying to insert into multiple tables using rails.. i have a table called users and services. when i create a user the user details should go into users and a service name and userid should go into services table. Any help would be greatly appriciated. Thanks. 回答1: You can add a callback to create a new service when a user is created. class User < ActiveRecord::Base def after_create Service.create!(:name => 'my_service_name', :user_id => self.id) end end The after_create method is

With Rails, how can I query the db for an aggregate number and grouped by week?

ぃ、小莉子 提交于 2019-12-08 00:44:49
问题 Given a table like so: Log: id, user_id, points, created_at With Rails, how can I query the database by user_id and then GROUPED by week via created_at where I can then having something like: WEEK of X/XX/XXXX - 3011 total points WEEK of X/XX/XXXX - 320 total points WEEK of X/XX/XXXX - 31 total points WEEK of X/XX/XXXX - 30330 total points Thanks 回答1: points_by_week = Log.where(user_id: user_id).group("DATE_TRUNC('year', created_at)", "DATE_TRUNC('week', created_at)").sum(:points) Will yield

ruby on rails getting a 2-way friend relationship in active record?

时间秒杀一切 提交于 2019-12-08 00:32:29
问题 Im trying to figure out how to do a mutual 2-way relationship, that is: user_id friend_id 1 2 2 1 In above user 1 and user 2 would be friends if both user_id = 1 has friend_id = 2 and friend_id = 2 has user_id = 2 as there friend in a table. How to count all the 2-way mutual relations in ActiveRecord? 回答1: What you're looking for is a has_and_belongs_to_many relationship: class User < ActiveRecord::Base has_and_belongs_to_many :friends, :class_name => "User", :foreign_key => "this_user_id",

How to delete some associations from an ActiveRecord object without saving

会有一股神秘感。 提交于 2019-12-07 22:36:07
问题 So I have: @project = Project.last @project.tasks = @project.tasks.reject{|o| o.stupid? } # Do more stuff When I update the tasks array, it actually saves it which we don't want. How do we prevent that? 回答1: The save happens when you reassign the result to @project.tasks . Just don't reassign it, and use reject! to change the original @project.tasks : @project = Project.last @project.tasks.reject!{ |o| o.stupid? } 来源: https://stackoverflow.com/questions/22927284/how-to-delete-some

Include all ids in ActiveRecord query

落花浮王杯 提交于 2019-12-07 22:29:31
问题 I am developing a gaming platform and I have the following (simplified) models: class Game < ActiveRecord:Base has_many :game_players has_many :players, through: :game_players end class Player < ActiveRecord:Base has_many :game_players has_many :games, through: :game_players end class GamePlayer < ActiveRecord:Base belongs_to :game belongs_to :player end I need to perform an ActiveRecord query that looks for all games played by a certain group of users. For example, given the data: +---------

Activerecord: find record by grandparent value

此生再无相见时 提交于 2019-12-07 22:07:24
问题 I have the following models: class GrandParent < ActiveRecord::Base has_many :parents has_many :children, through: :parents end class Parent < ActiveRecord::Base has_many :children belongs_to :grand_parent end class Child < ActiveRecord::Base belongs_to :parent end I'd like to find all Children where the a child's grand_parent has a value equal to TRUE, but I'm having trouble getting the syntax right. Something like: Child.where(grand_parent.value: TRUE) 回答1: You need to join the models in