activerecord

How does Rails keep track of which migrations have run for a database?

狂风中的少年 提交于 2019-12-17 15:24:36
问题 According to Rails doc: http://guides.rubyonrails.org/migrations.html "Active Record tracks which migrations have already been run so all you have to do is update your source and run rake db:migrate." How does ActiveRecord actually do this? Where does Active Record store the data? I suspect this might be stored in the database itself? In a table somewhere. On my development machine, I ran all the migrations. Then I copied the production database over using mysqldump. Then I ran "rake db

Converting an array of objects to ActiveRecord::Relation

若如初见. 提交于 2019-12-17 15:18:32
问题 I have an array of objects, let's call it an Indicator . I want to run Indicator class methods (those of the def self.subjects variety, scopes, etc) on this array. The only way I know to run class methods on a group of objects is to have them be an ActiveRecord::Relation. So I end up resorting to adding a to_indicators method to Array . def to_indicators # TODO: Make this less terrible. Indicator.where id: self.pluck(:id) end At times I chain quite a few of these scopes to filter down the

Rails - Best-Practice: How to create dependent has_one relations

独自空忆成欢 提交于 2019-12-17 15:05:21
问题 Could you tell me whats the best practice to create has_one relations? f.e. if i have a user model, and it must have a profile... How could i accomplish that? One solution would be: # user.rb class User << ActiveRecord::Base after_create :set_default_association def set_default_association self.create_profile end end But that doesnt seem very clean... Any suggests? 回答1: Best practice to create has_one relation is to use the ActiveRecord callback before_create rather than after_create . Or use

Rails best way to get previous and next active record object

你离开我真会死。 提交于 2019-12-17 11:43:38
问题 I need to get the previous and next active record objects with Rails. I did it, but don't know if it's the right way to do that. What I've got: Controller: @product = Product.friendly.find(params[:id]) order_list = Product.select(:id).all.map(&:id) current_position = order_list.index(@product.id) @previous_product = @collection.products.find(order_list[current_position - 1]) if order_list[current_position - 1] @next_product = @collection.products.find(order_list[current_position + 1]) if

Issue when retrieving records with empty array

一曲冷凌霜 提交于 2019-12-17 11:22:27
问题 I have a table of around 100 Users and I also have an array of user ids. What I wanted to do is show all users who are not a part of this array of user ids. When I do something like this User.where('id NOT IN (?)', [9, 2, 3, 4]) It successfully returns the records where the user's id does not belong in that array. However if that array is empty like so User.where('id NOT IN (?)', []) It does not return any users back and the SQL query looks like this SELECT "users".* FROM "users" WHERE (id

Log the actual SQL query using ActiveRecord with Yii2?

心已入冬 提交于 2019-12-17 10:33:36
问题 I'm doing this: $students = Student::find()->all(); return $this->render('process', array('students' => $students)); and then this in the view: foreach($students as $student) { echo $student->name . ',  '; echo $student->getQuizActivitiesCount(); ?> <br /> <?php } i would like to see the sql query being performed. a student "has many" quiz activities, and the query performs perfectly, but i need to see the raw SQL. is this possible? 回答1: Method 1 With relations that return yii\db\ActiveQuery

Rails 3.1 limit user created objects

旧城冷巷雨未停 提交于 2019-12-17 10:29:56
问题 I would like to limit the number of model Objects a user can create. I've tried the below but it is not working. I understand some changes have happened in rails 3.1 and not sure how to accomplish this now. class User < ActiveRecord::Base has_many :things, :limit => 5, :dependent => :destroy # This doesn't work end class Things <ActiveRecord::Base belongs_to :user end 回答1: Try something like this: class User < ActiveRecord::Base has_many :things end class Things <ActiveRecord::Base belongs_to

ActiveRecord Rails 3 scope vs class method

蓝咒 提交于 2019-12-17 10:24:15
问题 I'm new to the new query interface of ActiveRecord so I'm still figuring things out. I was hoping someone could explain the difference between using a scope in an ActiveRecord model and just using a class method (ie self.some_method ) From what I can gather, a scope is always expected to return a relation, whereas a class method doesn't necessarily have to. Is this true? For instance, I thought it would make sense to do something like: class Person scope :grouped_counts, group(:name).count

delete_all vs destroy_all?

荒凉一梦 提交于 2019-12-17 10:08:04
问题 I am looking for the best approach to delete records from a table. For instance, I have a user whose user ID is across many tables. I want to delete this user and every record that has his ID in all tables. u = User.find_by_name('JohnBoy') u.usage_indexes.destroy_all u.sources.destroy_all u.user_stats.destroy_all u.delete This works and removes all references of the user from all tables, but I heard that destroy_all was very process heavy, so I tried delete_all . It only removes the user from

HABTM Polymorphic Relationship

痞子三分冷 提交于 2019-12-17 09:32:49
问题 I'm pretty new to Rails, and i'm trying to do a polymorphic HABTM relationship. The problem is that I have three models that I want to relate. The first one is the Event model and then are two kind of attendees: Users and Contacts. What I want to do is to be able to relate as an attendee both users and contacts. So, what i have right now in my code is: Event Model has_and_belongs_to_many :attendees, :polymorphic => true User Model has_and_belongs_to_many :events, :as => :attendees Contact