activerecord

Codeigniter - Active record - sql - complex join

流过昼夜 提交于 2020-01-13 09:34:11
问题 I have a function that retrieves all tags from a table: function global_popular_tags() { $this->db->select('tags.*, COUNT(tags.id) AS count'); $this->db->from('tags'); $this->db->join('tags_to_work', 'tags.id = tags_to_work.tag_id'); $this->db->group_by('tags.id'); $this->db->order_by('count', 'desc'); $query = $this->db->get()->result_array(); return $query; } I have another table called 'work'. The 'work' table has a 'draft' column with values of either 1 or 0. I want the COUNT(tags.id) to

How to determine if association changed in ActiveRecord?

耗尽温柔 提交于 2020-01-13 08:37:10
问题 ActiveRecord offers change tracking, where calling #name_changed? returns true/false depending on whether the name attribute changed between when the model was loaded and now. Is there an equivalent for associations? I am specifically looking for has_many associations, but all association types would be useful. Rails 5.2, Ruby 2.5.1 回答1: Ok, not a answer but this is a workaround I came up with. In my project I was already using the gem audited to keep track of changes on the model I also want

How to list out all items in a nested table in Laravel

你离开我真会死。 提交于 2020-01-13 05:39:11
问题 I am using Laravel's Eloquent ORM and I'm having trouble eager loading items for display. Here is the scenario: Users follow Blogs Blogs have Posts I have a database table named Relationships, this table is used to store the User ID and the Blog ID to show which User is following which Blog. I have a table for Blogs describing the Blog and I have a table for Posts. The Relationships table would be my pivot table to connect the Users with the Blogs tables together. Now, I need to list out all

Should I use ON DELETE CASCADE, :dependent => :destroy, or both?

人走茶凉 提交于 2020-01-13 05:28:06
问题 In a Rails app, I have foreign key constraints in MySQL which I set them all up manually, separate from my migrations. I'm trying to figure out whether I should use ActiveRecord's :dependent => :destroy option. For example, in my schema I have tables... users ----- log_entries ----------- user_id # Has FK constraint to users.id with ON DELETE CASCADE And in my model I could have... class User < ActiveRecord::Base has_many :log_entries, :dependent => :destroy end Should I leave out the

Yii2 + Redis as Database

China☆狼群 提交于 2020-01-13 05:26:31
问题 I want to use Yii2 and redis as database. So far, I got Redis ActiveRecord Class for Yii2 from Here. link1 link2 but, I got a problem. WHY THIS CLASS ADDS ANYTHING AS HASH IN REDIS???? Above that I cant Find in which pattern it Insert data. I add one user and it will add a user under user:xxx namespace and another record under s:user:xxx and so on but none of theme has any fields that i defined in attributes!! only contain IDs. I know that a Key-value type database and RDBMS are different and

Using scope to return results within multiple DateTime ranges in ActiveRecord

和自甴很熟 提交于 2020-01-13 02:25:26
问题 I've got a Session model that has a :created_at date and a :start_time date, both stored in the database as :time . I'm currently spitting out a bunch of results on an enormous table and allowing users to filter results by a single date and an optional range of time using scopes, like so: class Session < ActiveRecord::Base ... scope :filter_by_date, lambda { |date| date = date.split(",")[0] where(:created_at => DateTime.strptime(date, '%m/%d/%Y')..DateTime.strptime(date, '%m/%d/%Y').end_of

Best practices for multiple associations with the same class in Rails?

大兔子大兔子 提交于 2020-01-13 01:49:49
问题 I think my question is best described as an example. Let's say I have a simple model called "Thing" and it has a few attributes that are simple data types. Something like... Thing - foo:string - goo:string - bar:int That isn't hard. The db table will contain three columns with those three attributes and I can access them with something like @thing.foo or @thing.bar. But the problem I'm trying to solve is what happens when "foo" or "goo" can no longer be contained in a simple data type? Assume

performant ordering of keys in a MySQL compound index (WRT Rails Polymorphic associations and STI)

一世执手 提交于 2020-01-12 07:53:30
问题 Previously, I asked this question about compound indexes on polymorphic foreign keys in ActiveRecord. The basis of my question was my understanding that indexes should be based on the cardinality of your column, and there's generally pretty low cardinality on Rails's STI type and polymorphic _type columns. Accepting that the answer to my question is right -- that's there's value to indexing both the high cardinality _id columns and the low cardinality _type columns, because they together they

ActiveRecord Association select counts for included records

放肆的年华 提交于 2020-01-12 04:38:08
问题 Example class User has_many :tickets end I want to create association which contains logic of count tickets of user and use it in includes (user has_one ticket_count) Users.includes(:tickets_count) I tried has_one :tickets_count, :select => "COUNT(*) as tickets_count,tickets.user_id " ,:class_name => 'Ticket', :group => "tickets.user_id", :readonly => true User.includes(:tickets_count) ArgumentError: Unknown key: group In this case association query in include should use count with group by .

activerecord - how to get all column of joined tables

走远了吗. 提交于 2020-01-12 03:27:07
问题 After reading : this I still don't get it. in console: puts Category.joins(:posts) It perform join or left join on Category and Post. However, all data returned is just columns in Category table. How to also get those column data in Post. ? should I make another model to achieve this ? after reading: this Is find_by_SQL is the only way ? I want ActiveRecord way if possible. Thanks. 回答1: You can get columns in posts tables by doing further querying, something like - Category.joins(:posts)