activerecord

Any possible way to add parameters to ON clause on include left joins on rails?

二次信任 提交于 2019-12-10 12:18:26
问题 I have a huge complex query like this: @objects = Object.joins({ x: :y }).includes( [:s, { x: { y: :z } }, { l: :m },:q, :w, { important_thing: [:h, :v, :c,:l, :b, { :k [:u, :a] }] } ]).where(conditions).order("x.foo, x.bar") Then i want to show all Objects and only Important_things that were created at between two dates. If i put this on there where clause i dont get all Objects , only Objects that has Important_things between informed dates. A solution using raw sql was this: select * from

ActiveRecord::Relation issue on acts-as-taggable-on

孤者浪人 提交于 2019-12-10 12:17:14
问题 I am a rails newbie. I am trying to implement acts-as-taggable-on on my sample app. I am able to enter multiple tags using tag_list but facing issues searching them. This is what I got. I used scaffold User to generate the controller & model. class CreateUsers < ActiveRecord::Migration def change create_table :users do |t| t.string :name t.text :tags t.timestamps end end end My User Model is class User < ActiveRecord::Base serialize :tags acts_as_taggable_on :tags scope :by_join_date, order(

Ruby on Rails joins models for json output

风流意气都作罢 提交于 2019-12-10 12:15:30
问题 I'm trying to build a JSON API end point with Ruby on Rails. I followed the instruction in the following and was able to create JSON API for my models http://railscasts.com/episodes/350-rest-api-versioning?view=comments/ I have the following controller: /api/v1/movies_controller.rb class MoviesController < ApplicationController def index if params[:p].nil? p = 1 else p = params[:p].to_i end @movies = Movie.order("id DESC").page(p) end def show @movie = Movie.find(params[:id]) end end I need

Ruby on Rails: Data not saving ActiveRecord

99封情书 提交于 2019-12-10 12:09:58
问题 I'm pretty new to Ruby on Rails and I've been trying to develop a simple blog. However when I try to save the new Post, the page reloads a new page and no data is saved. The data however is present in the URI. Here's my controller: class PostsController < ApplicationController before_action :set_post, only: [:show, :edit, :update, :destroy] # GET /posts # GET /posts.json def index @posts = Post.all end # GET /posts/1 # GET /posts/1.json def show end # GET /posts/new def new @post = Post.new

Generating reports efficiently with ActiveRecord

偶尔善良 提交于 2019-12-10 11:55:19
问题 We need to generate reports that pull in lots of data, run some calculations and spit them out as part of a larger table. Doing this isn't difficult. However, making it so that existing methods can be used and not generating 1000's of SQL queries is a lot harder. For instance I might have an Account class with a method like this: def balance_at(time=Time.now) payments_out = self.payments.where("created_at <= ?",time).sum("amount") payments_in = self.payments_on_account.where("created_at <= ?"

Routing error in activerecord reputation gem in rails 4

眉间皱痕 提交于 2019-12-10 11:48:33
问题 Hello I'm new to rails 4 I am working on project where we can post and rate it. I have used activerecord reputation gem. I have been following the http://railscasts.com/episodes/364-active-record-reputation-system video. I am stuck at the routing point. When I click on upvote or downvote it shows me error. my routes.rb file has resources :posts do resources :comments, :only => [:create] member { post :vote } end get "posts/create" get "posts/destroy" get "posts/new" get "posts/index" get

Rails 3, locking tables on an auction type engine

做~自己de王妃 提交于 2019-12-10 11:47:01
问题 I'm building an auction-like system in Rails 3 (using PostgreSQL as the database). Imagine you have a Product. A product has many Bids. I'm worried with what happens when 2 different users click "Bid" at the same time (providing I have at least 2 application servers, and they hit the servers at the same time). I have two possible acceptable behaviors for this: one of them "wins", and the other receives an error message one of them "wins", and the other bid is created next (each bid adds 0.01€

Editing multiple rows in a table (updating multiple records)

元气小坏坏 提交于 2019-12-10 11:46:23
问题 I have a list of payment records in Payments#index. I've been asked to make that table directly editable and to have one button that saves all the updates. So far I've only partially managed to do this by wrapping each row around a form_for for that record like so: <tbody> <% @payments.each do |payment| %> <tr> <td><%= payment.client.trading_name %></td> <td><%= payment.date_of_payment %></td> <td> <%= form_for payment do |f| %> <%= f.text_field :amount %> <%= f.submit 'Save', class: 'table

validates :something, :confirmation => true & attr_accessor confusion

馋奶兔 提交于 2019-12-10 11:38:34
问题 am struggling with Ruby validates :confirmation => true in my Rails app. Consider the following code: # == Schema Information # # Table name: things # # id :integer not null, primary key # pin :integer(8) # created_at :datetime # updated_at :datetime # class Things < ActiveRecord::Base #attr_accessor :pin attr_accessible :pin, :pin_confirmation validates :pin, :confirmation => true, :length => { :within => 7..15 }, :numericality => { :only_integer => true } end As the code is above, my

ruby - getting records filtered by date

馋奶兔 提交于 2019-12-10 11:33:10
问题 i have a "Job" module (and corresponding table in the db), that module have a field called scheduled_run (datetime) and a field called user_id: how can i get all the jobs that belongs to a user and are scheduled for today? how can i get all the jobs that belongs to a user and are scheduled for last week? 回答1: To get all the jobs that belong to a user and are scheduled for today, you'll need to search for all scheduled_runs within the today time period: user = User.find(1) # Will get the user