activerecord

Why group calculation fields do not show up in query result?

若如初见. 提交于 2019-12-11 06:53:02
问题 I have query like this: query = Link.select('url, max(created_at) as created_at, count(*) as url_count').group(:url).order('url_count desc, created_at asc') Sample results of query.results.first : 2.2.0 :002 > query.first => #<Link id: nil, url: "http://1", created_at: "2015-03-10 16:43:54"> Why there is no url_count here, even though I know it is. 2.2.0 :003 > query.first.url_count => 17 回答1: The count is there all along but the model to_s method does not know about it. The to_s method which

In Ruby, with ActiveRecord what is the best way to retrieve an object that belongs_to a parent object, given some conditions?

亡梦爱人 提交于 2019-12-11 06:51:14
问题 In my model an Organisation has_many :users , and a User has_and_belongs_to_many :roles and a Role has a name and has_and_belongs_to_many :users . In my Organisation class I have a method get_admin that is supposed to get the User belonging to that Organisation who has the Role 'admin' . Like so: def get_admin return self.users.with_role('admin') end Alas this returns an ActiveRecord::Relation object, not a User . I tried appending .first to the end of the line like so def get_admin return

Does ActiveRecord assign a key to every table using the naming convention “ID”, and if so, why?

空扰寡人 提交于 2019-12-11 06:49:45
问题 My understanding is that Actice Record is based on a object-relational mapping (ORM) pattern described by Martin Fowler in his book Pattern of Enterprise Application Architecture (Addison-Wesley, 2002); which states a one-to-one mapping relationship exists between a database record and the object that represents it in an object-oriented program (OOP). When Rails creator David Heinemeier sought to implement an ORM for his Rails framework, he based it on Fowler's pattern. Here's the problem,

belongs_to on activerecord rendering as not found

这一生的挚爱 提交于 2019-12-11 06:42:26
问题 I must be doing something stupid here. I am using rails 3.2.19 with activeadmin 0.6.0. Basically I'm trying to do a navigate down through a belongs_to association via a side bar. I have loaded my database with text fixtures and can drill down through the association at the rails console, i.e: 2.1.1 :004 > Employee.first.blogposts.first Employee Load (0.1ms) SELECT "employees".* FROM "employees" LIMIT 1 Blogpost Load (0.1ms) SELECT "blogposts".* FROM "blogposts" WHERE "blogposts"."employee_id"

Conditional clauses using Codeigniter's Active Record

落花浮王杯 提交于 2019-12-11 06:32:36
问题 I have a function returning data based on the parameters passed. Basically, the queries change depending on the clauses. if(1==$case) return select()->from('db')->where('x',$something)->where('y','fixed')... else if(2==$case) return select()->from('db')->where('x','fixed')->where('y',$something)... Is there a better way of doing this? Also, is it possible to have such a clause ...->where('category',*) '*' to be replaced by a value or something which equates to "any". 回答1: This type of queries

Named Scope to only search first results of joined table

此生再无相见时 提交于 2019-12-11 06:31:15
问题 I'm having trouble with a named scope, SQL not my strong suit. I would like to return ALL Machines which had it's LAST test fail. My Machines model: has_many :lodged_tests, :dependent => :destroy has_one :last_test, :class_name => 'LodgedTest', :order => 'created_at DESC' named_scope :last_test_failed, :joins => :last_test, :conditions => [ "lodged_tests.is_passed = ?", false] The named_scope does work except it returns Machines which have ANY failed tests. I need it to return machines which

belongs_to has_one structure

血红的双手。 提交于 2019-12-11 06:29:34
问题 I have an application which has the following characteristics There are Clubs Each Club has Teams Each Team has Players I have a users table. The user table basically contains the username and password for the club manager, team manager and the player to login to the system. How should I structure the models and the tables? I plan to create tables for Club, Team and Players. But I am not sure show to structure the relationship between them and the users table. I could create user_id in each

Express a CTE using Arel

旧街凉风 提交于 2019-12-11 06:17:41
问题 I have this snippet of ActiveRecord: scope = Listing.where(Listing.arel_table[:price].gt(6_000_000)) The resulting sql: SELECT listings.* FROM listings where listings.price > 6000000 I would like to add a CTE that would result in this sql: WITH lookup AS ( SELECT the_geom FROM lookup WHERE slug = 'foo-bar' ) SELECT * from listings, lookup WHERE listings.price > 6000000 AND ST_within(listings.the_geom, lookup.the_geom) I would like to express this sql inclusive of the CTE using Arel and

Using active record and mysql on ruby script

Deadly 提交于 2019-12-11 06:17:08
问题 I've been trying to use mysql and active record on a ruby script but I'm getting the following message: Outdated mysql gem. Upgrade to 2.8.1 or later. In your Gemfile: gem 'mysql', '2.8.1'. Or use gem 'mysql2' (RuntimeError). Before I was getting a message about gem 'mysql2' missing but then i checked the source code on the connection adapters in active record and saw that it require mysql gem which i didnt have installed. this is my script: require 'rubygems' require 'active_record' require

Is storing the data as json in mysql a good idea?

淺唱寂寞╮ 提交于 2019-12-11 06:16:16
问题 I got a scenario to have relational tables like a "section has many elements", so I thought of going for has_many relation model like a section has many elements. I got some suggestions from our client saying to save the data as JSON inside a column instead of multiple tables. I am bit confused which approach I should go with. Suggestions are welcome. This is the model am proposing eg : section has_many elements Table : sections id type 1 page Table : elements id type content section_id 1