activerecord

How do I do reflexive self-join relationships in ActiveRecord?

萝らか妹 提交于 2019-12-18 04:25:08
问题 I'm trying to implement a social networking style friendship model and I didnt have much much luck trying to figure out the plugins available out there. I think I'll learn Rails better if I do it myself. So here's what I have : class User < ActiveRecord::Base has_many :invitee_friendships , :foreign_key => :friend_id, :class_name => 'Friendship' has_many :inviter_friends, :through => :invitee_friendships has_many :inviter_friendships , :foreign_key => :user_id, :class_name => 'Friendship' has

Yii2 ActiveRecord cache

 ̄綄美尐妖づ 提交于 2019-12-18 04:22:40
问题 How to use ActiveRecotd cache for Yii 2? I did't find any examples in official docs. In Google I found 2 examples, first is: $db = self::getDb(); $object = $db->cache(function ($db) use($id) { return self::findOne($id); }); But it doesn't work for Model , I tested with updated framework. Other example is: $data = \Yii::$app->cache->get('some_var_' . $id); if ($data === false) { $data = self::findOne($id); \Yii::$app->cache->set('some_var_' . $id, $data, 60); } It's working fine, but it's not

ActiveRecord SQL execution time

最后都变了- 提交于 2019-12-18 04:09:17
问题 How can I get the SQL query execution time in rails? I can see the time in logs, e.g.: Posting Load (10.8ms) SELECT "postings".* FROM "postings" ORDER BY "postings"."id" ASC LIMIT 1 But how can I get that value (10.08) programmatically to use it further in my code? 回答1: You can use ActiveSupport::Notifications to retrieve the runtime of the SQL statements You should be able to instrument sql.active_record to see how long the SQL call takes ActiveSupport::Notifications.subscribe('sql.active

ActiveRecord connection warning. (Database connections will not be closed automatically)

孤街浪徒 提交于 2019-12-18 04:04:24
问题 I'm trying to create a little app with Sinatra and ActiveRecord (3.2.3). This is how my main file looks like: require "sinatra" require "sinatra/reloader" require "active_record" ... ActiveRecord::Base.establish_connection( adapter: 'sqlite3', database: 'db.sqlite3', host: 'localhost', ) class Post < ActiveRecord::Base ... end get('/') { ... } get('/posts') { ... } ... It works, but sometimes I get a warning in console: DEPRECATION WARNING: Database connections will not be closed

Rails 3 Limiting Included Objects

帅比萌擦擦* 提交于 2019-12-18 03:55:39
问题 For example I have a blog object, and that blog has many posts. I want to do eager loading of say the first blog object and include say the first 10 posts of it. Currently I would do @blogs = Blog.limit(4) and then in the view use @blogs.posts.limit(10) . I am pretty sure there is a better way to do this via an include such as Blog.include(:posts).limit(:posts=>10) . Is it just not possible to limit the number of included objects, or am I missing something basic here? 回答1: Looks like you can

Using named_scope with counts of child models

瘦欲@ 提交于 2019-12-18 03:45:54
问题 I have a simple parent object having many children. I'm trying to figure out how to use a named scope for bringing back just parents with specific numbers of children. Is this possible? class Foo < ActiveRecord::Base has_many :bars named_scope :with_no_bars, ... # count of bars == 0 named_scope :with_one_bar, ... # count of bars == 1 named_scope :with_more_than_one_bar, ... # count of bars > 1 end class Bar < ActiveRecord::Base belongs_to :foo end I'm hoping to do something like Foo.with_one

Store the day of the week and time?

谁都会走 提交于 2019-12-18 03:45:28
问题 I have a two-part question about storing days of the week and time in a database. I'm using Rails 4.0, Ruby 2.0.0, and Postgres. I have certain events, and those events have a schedule. For the event "Skydiving", for example, I might have Tuesday and Wednesday and 3 pm. Is there a way for me to store the record for Tuesday and Wednesday in one row or should I have two records? What is the best way to store the day and time? Is there a way to store day of week and time (not datetime) or should

Using named_scope with counts of child models

南楼画角 提交于 2019-12-18 03:45:25
问题 I have a simple parent object having many children. I'm trying to figure out how to use a named scope for bringing back just parents with specific numbers of children. Is this possible? class Foo < ActiveRecord::Base has_many :bars named_scope :with_no_bars, ... # count of bars == 0 named_scope :with_one_bar, ... # count of bars == 1 named_scope :with_more_than_one_bar, ... # count of bars > 1 end class Bar < ActiveRecord::Base belongs_to :foo end I'm hoping to do something like Foo.with_one

Store the day of the week and time?

梦想与她 提交于 2019-12-18 03:45:01
问题 I have a two-part question about storing days of the week and time in a database. I'm using Rails 4.0, Ruby 2.0.0, and Postgres. I have certain events, and those events have a schedule. For the event "Skydiving", for example, I might have Tuesday and Wednesday and 3 pm. Is there a way for me to store the record for Tuesday and Wednesday in one row or should I have two records? What is the best way to store the day and time? Is there a way to store day of week and time (not datetime) or should

How to manage opening and closing database connections while working with activerecords and multiple threads

二次信任 提交于 2019-12-18 02:46:28
问题 I am trying to implement a multithreaded method in rails such that I can create/update multiple records very quickly. This is the outline of my program. ActiveRecord::Base.transaction do (1..10).each do |i| arr[i] = Thread.new { <some logic on obj> ... ... obj.save! } end arr.each {|t| t.join} end This gives me warnings on my log. DEPRECATION WARNING: Database connections will not be closed automatically, please close your database connection at the end of the thread by calling `close` on