activerecord

Rails 4 x paper_trail: filter versions by item_id with nested resources

我们两清 提交于 2019-12-12 18:28:24
问题 In my Rails 4 app, I have the following models: class User < ActiveRecord::Base has_many :administrations, dependent: :destroy has_many :calendars, through: :administrations end class Administration < ActiveRecord::Base belongs_to :user belongs_to :calendar end class Calendar < ActiveRecord::Base has_many :administrations, dependent: :destroy has_many :users, through: :administrations end class Post < ActiveRecord::Base belongs_to :calendar end I installed the paper_trail gem to track changes

Rail's ActiveRecord find_each with DISTINCT select

穿精又带淫゛_ 提交于 2019-12-12 18:25:33
问题 I want to get a list of all unique emails I have in my database and process them in batch using find_each . The code below works fine until it has more then 1000 records (batch size) to process. Then it breaks after the 1000th record with the error message Primary key not included in the custom select clause Tourist.select('DISTINCT email').where("DATE(created_at) = ?", Date.today- 1).find_in_batches do |group| something end So, how can I chain all this: I only need a specific field (email) I

Using 'end' as column name in Ruby on Rails (MySQL)

情到浓时终转凉″ 提交于 2019-12-12 18:12:11
问题 I had an model with an "end" column (datetime format), only to discover that Heroku crashes and burns with illogical Active Record errors whenever I attempted to reference the column in a query. I spent two hours trying to debug the extremely simple query, after which point I renamed the column to "end_at" and all of my problems disappeared. Has anybody else experienced this issue? I'm curious of the reasoning behind this and hope that we can help others avoid the same mistake. A similar

Activerecord query to include count of associations

为君一笑 提交于 2019-12-12 17:26:17
问题 I have a model User and it has many posts. To query first 5 users, I do: User.all :limit => 5 which returns a user which is good, but when getting the count of # of posts each user has, it queries the DB again: a_user.posts.count Is there a way I can avoid this query and get the count with the first query ? I am looping through users and it blocks each time it query for user's post's count. 回答1: Rails has a built in method, that has to be set to true when declaring associations like has_many,

ActiveRecord: Get all users who should be banned today

倖福魔咒の 提交于 2019-12-12 17:24:22
问题 I have two models: class User < ActiveRecord::Base has_many :ban_messages, dependent: :destroy end class BanMessage < ActiveRecord::Base belongs_to :user end Table BanMessage contains a field t.datetime :ban_date that stores the date, when user should be banned. User table contains a field status , that contains one of the status values (active, suspended, banned). When I send BanMessage to User, his status field changes from 'active' to 'suspended'. Also I can activate user back, so he would

Invalid index n for this SqlParameterCollection with Count=m

点点圈 提交于 2019-12-12 17:17:57
问题 I'm getting this nasty error in Castle Active Record (wrapped around NHibernate) when I try to save a class: Invalid index n for this SqlParameterCollection with Count=m I know that this error is caused by a property being mapped multiple times in a class however I'm not sure how to get around it. I have two child classes that both map back to the class in question using the same column (IpAddressNumber). Also IpAddressNumber is the primary key of the class, which results in NHibernate trying

Yii2 Modify find() Method in Model search()

可紊 提交于 2019-12-12 15:49:38
问题 I am trying to modify the find() method inside the model search and it throws an error "The data provider property must be set". Here is my search model: public function search($params) { $userID = Yii::$app->user->identity->id; $groups = GroupAccess::find() ->where(['user_id' => $userID, 'item_name' => 'group_creator']) ->asArray() ->all(); foreach ($groups as $group) { $accessGroups[] = $group['group_id']; } $query = Group::find($accessGroups); $dataProvider = new ActiveDataProvider([

ActiveRecord boolean validation accepting non-boolean values

邮差的信 提交于 2019-12-12 15:44:27
问题 I'm trying to validate that an attribute is a boolean, i.e. true or false. From the Rails guide I expected validates :new_out_of_stock, inclusion: { in: [true, false] } to work, but it accepts non-boolean values (e.g. "Hi") as valid: irb> ip = ItemPrice.new irb> ip.new_out_of_stock = "Hi" => "Hi" irb> ip.valid? => true irb> ip.errors.messages => {} It correctly rejects nil and accepts true and false. All the documentation on the Internet that I've read says that this is the correct way to

Include, Select, Sort, Limit from multiple models (single query)

一曲冷凌霜 提交于 2019-12-12 15:10:26
问题 I need to create a single query that includes data from the following tables: *Conversation : a model that groups messages between users class Conversation < ActiveRecord::Base # Associations has_many :messages, dependent: :destroy has_many :conversation_participants has_many :users, :through => :conversation_participants ## Attributes title, created_at, updated_at end * ConversationParticipant : a model that keeps track of the users of the conversation class ConversationParticipant <

Active Record Query where value in array field

佐手、 提交于 2019-12-12 15:00:29
问题 I have a table called " Stat " in my MongoDB database in Rails 3 . In that table, there is an array field called "services" . I want to find all Stats that have a services array that contains the value "lights" . I want to do something like this : @stats = Stat.all @stats1 = @stats.where("services contains lights") Rails.logger.info "result: #{@stats1.count} " I've tried various things and Googled it extensively, found some leads but nothing that seems to work. I have four records that should