activerecord

Conditional order for nested model based on field

北慕城南 提交于 2019-12-11 12:11:16
问题 I have an Event model which has_many Results. I want to order the results based on a field in Event. I currently have in Event: has_many :results, :dependent => :destroy, :include => [:event], :order => "IF(self.etype = 'Stroke', 'results.score ASC', 'results.score DESC')" ...but this deosn't work. Is there a better way to do this with named_scope for example? Sorry for my stupidy, I am new to Rails. 回答1: Try this: has_many :results, :dependent => :destroy, :include => [:event], :order => '#{

Rails will_paginate shows duplicates on HABTM models

半城伤御伤魂 提交于 2019-12-11 11:39:26
问题 I'm using will_paginate with a HABTM relationship between Blog Posts and Tags. Whenever I apply pagination, I get duplicate posts shown because the HABTM in Rails doesn't keep the database unique, it applies the uniqueness upon making the query. blog_posts.rb has_and_belongs_to_many :tags, :uniq => true tag.rb has_and_belongs_to_many :blog_posts, :uniq => true Per the documentation for ActiveRecord, :uniq does not prevent duplicate relationships being stored, it just ignores them when

Rails have ActiveRecord grab more than one association in one go?

左心房为你撑大大i 提交于 2019-12-11 11:25:58
问题 The question below had a good answer to grab associated values of an activerecord collection in one hit using Comment.includes(:user) . What about when you have multiple associations that you want to grab in one go? Rails have activerecord grab all needed associations in one go? Is the best way to just chain these together like below Customer.includes(:user).includes(:sales).includes(:prices) or is there a cleaner way. Furthermore, when I am doing this on a loop on an index table. Can I add a

Why ActiveRecord::StatementInvalid: SQLite3::SQLException: near “)”: syntax error: INSERT INTO “user_friendships” () VALUES ()

旧巷老猫 提交于 2019-12-11 11:22:29
问题 Why Why ActiveRecord::StatementInvalid: SQLite3::SQLException: near ")": syntax error: INSERT INTO "user_friendships" () VALUES () When trying to test: $ ruby -I test test/unit/user_friendships_test.rb require 'test_helper' class UserFriendshipsTest < ActiveSupport::TestCase should belong_to (:user) should belong_to (:friend) test "that creating a frinedship works without raising an exception" do assert_nothing_raised do UserFriendship.create user: users(:a), friend: friends(:b) end end end

Rails nested delegate attributes not being updated

大憨熊 提交于 2019-12-11 10:57:28
问题 Models class User < ActiveRecord::Base has_one :meta, class_name: SeoMetum, as: :metumable, dependent: :destroy delegate :browser_title, :browser_title=, :meta_description, :meta_description=, to: :meta has_one :collection accepts_nested_attributes_for :collection after_save :save_meta_tags! def save_meta_tags! meta.save end end class Collection < ActiveRecord::Base has_one :meta, class_name: SeoMetum, as: :metumable, dependent: :destroy delegate :browser_title, :browser_title=, :meta

Why my textbox shows ActiveRecord_Associations_CollectionProxy in form

血红的双手。 提交于 2019-12-11 10:36:54
问题 I'm building rails application and I use form_for to render the form. Everything works fine works fine except one text field has ActiveRecord_Associations_CollectionProxy inside the text which I'm not sure where it's coming from. Here's my form <%= form_for(@video, html: { class: "directUpload" }, multipart: true) do |f| %> <% if @video.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@video.errors.count, "error") %> prohibited this user from being saved:</h2> <ul> <% @video

heroku error - NoMethodError (undefined method `updated?'

吃可爱长大的小学妹 提交于 2019-12-11 10:27:19
问题 I've a rails 3.1 app on Heroku, I keep getting an 500 error in production which I can't recreate in dev. When I try to perform an update action in one of my controllers I get a 500. I get the following from heroku logs - 2011-12-05T13:52:35+00:00 app[web.1]: Completed 500 Internal Server Error in 15ms 2011-12-05T13:52:35+00:00 app[web.1]: 2011-12-05T13:52:35+00:00 app[web.1]: NoMethodError (undefined method `updated?' for #<ActiveRecord::Associations::HasOneAssociation:0x00000004058800>):

Generating the input id with an ActiveRecord model

≡放荡痞女 提交于 2019-12-11 10:14:36
问题 How do you generate an input's id attribute, given a model? For example, if I have a model of Person with a first_name attribute, the form helper prints out a textbox with this html: <input type="text" id="person_first_name" /> How can I generate that person_first_name from some other place in the code (like in a controller or some place)? 回答1: A good habit is to dig rails' code every now and then :) These values are generated by private methods inside the nodoc-ed InstanceTag class. You can

Rails - SQL query returning all objects, which have all specified related objects (has_many through)

*爱你&永不变心* 提交于 2019-12-11 10:06:24
问题 this is my first question here. I want to build a query which will give me all Themes with all specified Features. I have three models Theme, Features, and ThemeFeatures Theme has_many :features, :through => :theme_feautures Lets say there is one Theme object (id:1, name:"theme_a") in DB. It has (many) features [(id: 1, name:"feature_a"), (id: 2, name:"feature_b"), (id: 3, name:"feature_c")] The query should work like this: Theme.the_query(:features => {:id => [1,2]}) , result is Theme , but

Why am I getting deadlocks? Using Thread, Queue, and ActiveRecord. Ruby 1.9.3, Rails 2.3.18

£可爱£侵袭症+ 提交于 2019-12-11 09:46:37
问题 I mitigate the low success rate of a particular operation by running multiple threads and waiting for the first one to return with a valid answer. I've created a minimal example below: THREADS = [] if !defined?(THREADS) def threadtest THREADS.clear queue, mutex, result = Queue.new, Mutex.new, nil 10.times do |i| THREADS << Thread.new do counted = (10e6 + rand(10e6)).to_i.times { } # randomize computation time num = rand(8) # succeed only 1/8 of the time #mutex.synchronize do # even if wrapped