rails-activerecord

Adding a JOIN between two tables

て烟熏妆下的殇ゞ 提交于 2019-12-12 03:53:37
问题 Here is the classes I have: Model Organization has_many Students Model Student has_many Classes belongs_to Organization Model Class a field named : price belongs_to Student scope :top_expensive_classes, joins(:students).order('price DESC') Now I want to list the top 10 expensive classes At least the first problem I have is that in the params I have the organization_id to filter based on that But I write my controller like this which does NOT work because it thinks it should find organization

Modify created_at in query before grouping by date

a 夏天 提交于 2019-12-12 03:48:38
问题 I have this query that I am using to grab the purchases made by a particular user that is then mapped to a hash so it can be displayed in a graph. orders_graph = user.orders.where(:purchased_period => current_period). group("date(created_at)"). select("purchased_at, sum(total_price) as total_price") (start_time.to_date..Date.today).map do |date| order = orders_by_day.detect { |order| order.created_at.in_time_zone("#{user.time_zone}").to_date == date } order && order.total_price.to_f || 0

Rails query by record association

余生长醉 提交于 2019-12-12 02:42:42
问题 I'm relatively new to Rails and to learn more am trying to build a simple blog. One of the issues I'm running in to is tagging. I have a Tag model that I am using to create and manage tags that can be applied to posts. I also have a Post model. I need to be able to associate any of the tags with the post, retrieve them for output, and be able to filter/query posts by a specific tag. So far I've created a column on Post called tags , which gets assigned an array of Tag IDs, and is then

Multiple limit condition in mongodb

江枫思渺然 提交于 2019-12-12 02:06:42
问题 I have a collection in which one of the field is "type". I want to get some values of each type depending upon condition which is same for all the types. Like I want 2 documents for type A, 2 for type B like that. How to do this in a single query? I am using Ruby Active Record. 回答1: Generally what you are describing is a relatively common question around the MongoDB community which we could describe as the "top n results problem". This is when given some input that is likely sorted in some

Rails: why time datatype start at 1/1/2000

会有一股神秘感。 提交于 2019-12-12 01:59:08
问题 I have a column that I only want to store time (hour, minute, second). for example here is one migration: add_column :products, :start_hour, :time When I insert to database start_hour always start with 1/1/2000 for example: 2000-01-01 01:19:00 +0700 . Please explain for me why. 回答1: There is no time-of-day class in Ruby or Rails so ActiveRecord represents time columns with datetime/timestamp classes. If you look inside the database without any of the ActiveRecord, Rails, or Ruby noise in the

Rails::ActiveRecord preserve auto_increment id in deep copy duplicate

大城市里の小女人 提交于 2019-12-11 16:55:49
问题 I need to create a temporary array containing model objects to manipulate. I need the auto_increment primary key but I do not want the associations kept because when I remove the object from the array it removes it from the database as well (thanks rails, pretty inconvenient). The deep copies I create using object.dup have the id's marked as nil. How do I keep them in the copy? 回答1: dup describes this behaviour: Duped objects have no id assigned and are treated as new records. The dup method

How do I migrate data from production db to development db (Rails 4)?

落爺英雄遲暮 提交于 2019-12-11 12:35:03
问题 I know this one sounds kind of backwards! I've been working on a cloud-based case-management application, and was working on a support-ticketing feature. We have our development database (MySQL) that has all the same data as our production database (It's a very large app). Development is basically a "sandbox" environment, hence why the development db has all the same as the production db. This morning, I ran into a problem in my local development server for: Migrations are pending; run 'bin

Multiple Select with Rails Create Action

半世苍凉 提交于 2019-12-11 10:52:02
问题 I have a Collection_select, with Multiple set to true #views/courses/new <%=collection_select(:course, :department_id, Department.all, :id, :name, {}, :multiple =>true,:size => 8,:class=> "text")%> in My Models #deparment Model has_many :courses #Course Model belongs_to :deparment I want a situation such that if a course has more than one department selected from the Multiple select list, this details is saved in the course Table. My Current Implementation saves just the first selected

Rails Advance query CASE WHEN method not working

痞子三分冷 提交于 2019-12-11 10:14:48
问题 Is there anyone who can suggest me the solution for a Find_by_sql query here when I am using mysql that work perfect . But when I converted that into active record It is not showing the exact result . First I have tried this code below using find_by_sql: @message3 = Message.find_by_sql(["SELECT u.id,c.m_id,u.name,u.email FROM messages c, users u WHERE (CASE WHEN c.user_one = :id THEN c.user_two = u.id WHEN c.user_two = #{current_user.id} THEN c.user_one= u.id END ) AND ( c.user_one ='1' OR c

Ruby on Rails ActiveRecord uniqueness validation

China☆狼群 提交于 2019-12-11 10:04:14
问题 I'm pretty new to Rails and was reading some resources on the subject. And I got some questions on it. Ok, let's say we do have a model, that validates some attribute uniqueness: class User < ActiveRecord::Base attr_accesible :name validates :name, uniqueness: true end So, I reasonably expect now, that I will not be able to create (or to be more precise -- save) two User instances with the same name into the database. However, this resource does insist, that it is still possible! Some user