activerecord

where condition from a comma seperated varchar in mysql and codeigniter

谁说我不能喝 提交于 2019-12-24 12:48:40
问题 i have two tables called musical_types and musical_albums in which musical_types consists of type_name and an auto increment id, where as in the musical_albums table contains auto increment id , name , types which is a varchar which stores the id of musical_types as comma seperated values like 1,2,3,4 etc. So could anyone please help me in getting all the albums of type x from musical_albums table. I tried mysql like but it is not correct. so please help me in writing this query.. iam using

Rails 4 x paper_trail: keep track of versions after item is destroyed

六月ゝ 毕业季﹏ 提交于 2019-12-24 12:41:34
问题 This is a follow up question on Rails 4 x paper_trail: filter versions by item_id with nested resources. ————— CONTEXT FROM PREVIOUS QUESTION 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

Converting an SQL query to a Rails AREL query?

自作多情 提交于 2019-12-24 12:39:32
问题 I have a working SQL query thanks to the help of Erwin Brandstetter in my earlier question 'Order with a has_many relationship'. How would I turn this SQL into an ActiveRecords or AREL query for use in a scope? SELECT a.* FROM articles a LEFT JOIN ( SELECT DISTINCT ON (article_id) article_id, value FROM metrics m WHERE name = 'score' ORDER BY article_id, date_created DESC ) m ON m.article_id = a.id ORDER BY m.value DESC; The closest I have come is with a friends help... scope :highest_score,

Ruby on Rails: Why do I get timezones munged when I write a time to the DB, then read it back?

℡╲_俬逩灬. 提交于 2019-12-24 12:24:17
问题 I have config.time_zone in environment.rb set to "UTC", and my mySQL server returns the current time in my local time zone when I issue "select now();" and in utc when I ask for "select utc_timestamp;" I'm running rails 2.1.2, the mysql gem 2.7.3, activerecord gem 2.1.2, and mysql --version returns "Ver 14.12 Distrib 5.0.27 for Win32 (ia32)". EDIT: My environment.rb is set to UTC and had been since I started the project. A server restart would have picked up no changes. record = Record.find(

rails: how do I build an active-relation scope to traverse many tables?

你。 提交于 2019-12-24 11:59:58
问题 I have these tables and relationships: user has_many projects project has_many tasks task has_many actions I would like to build a scope that allows me to select all of the current users actions, regardless of what project or task they belong to. Thanks 回答1: I don't think scopes are necessary for this if you use the nested_has_many_through plugin. class User < ActiveRecord::Base has_many :projects has_many :tasks, :through => :projects has_many :actions, :through => :tasks end class Project <

ActiveRecord — Query all and include association which meets condition

五迷三道 提交于 2019-12-24 11:37:53
问题 Environment : Rails 3.2.22 Question : Lets say I have the models Topics , Posts , and User . Posts belongs to Topics User has many Posts I want to make a query of Topic.all , but includes all posts associated to a user. I've tried include and eager_load with a where condition for the user id, but only topics with a post which meets the condition are return. What I want is all topics return and include only posts which match the user_id condition. 回答1: After playing around with ActiveRecord I

Scope for an optional has_one association with itself

萝らか妹 提交于 2019-12-24 10:44:37
问题 I have a model Indent. On which I am using STI. An indent can be of two types Sale and Purchase. In purchase class I am using an optional has_one association. class Purchase < Indent has_one :sale , :class_name => 'Sale', :foreign_key => 'linked_indent_id' # Make it work. scope :unsold, lambda {includes(:sale).where('id not in (select distinct linked_indent_id from indents)')} end class Sale < Indent belongs_to :purchase , :class_name => 'Purchase', :foreign_key => 'linked_indent_id' end I

when doing a ActiveRecord::Base count how do i order by count desc?

耗尽温柔 提交于 2019-12-24 10:25:56
问题 an example would be... Pets.find(:all, :select => 'count(*) count, pet_type', :group => 'pet_type', :order => 'count') returns the correct results but not the actual counts in the orderedhash object returned. Pets.count(:all, :group => 'pet_type') returns the count but are not sorting in a descending fashion... how would i do this? I think i'd prefer to use .find .. but i'll take .count if i can sort it. 回答1: Pets.find(:all, :select => '*, count(*) AS count, pet_type', :group => 'pet_type',

Particular one-to-many relation

ⅰ亾dé卋堺 提交于 2019-12-24 09:51:26
问题 I've got a Rails 3.1 app with a User model and a House model (this is like a group). I've set up a many-to-many relation with join model Membership between those two and there are methods to manages roles of a user in some house in the join model. But my problem is that a User has only one house and not many. So I always have to do user.houses.first to get his house (I've set up a helper house which does that) but the design is not good so I've tried to put has_one :membership and has_one

Rails have activerecord grab all needed associations in one go?

China☆狼群 提交于 2019-12-24 09:28:00
问题 I have a model Comment, which has_a User. In my display page, I am doing a Comment.all , and then displaying each comment. In the view I need to display not only the comment, but also information about the associated user (ie the author). <% @comments.each do |comment| %> <%= comment.user.name %> <%= comment.user.email %> ...etc... <% end %> This is fine and all, but activerecord translates this into one SELECT * FROM users WHERE USER.id = commentId query per EACH comment I have. This is a