activerecord

Rails ActiveRecord :joins with LEFT JOIN instead of INNER JOIN

我与影子孤独终老i 提交于 2019-12-17 08:53:31
问题 I have this code User.find(:all, :limit => 10, :joins => :user_points, :select => "users.*, count(user_points.id)", :group => "user_points.user_id") which generates following sql SELECT users.*, count(user_points.id) FROM `users` INNER JOIN `user_points` ON user_points.user_id = users.id GROUP BY user_points.user_id LIMIT 10 is it possible to make LEFT JOIN instead of INNER JOIN other way than User.find_by_sql and manualy typing the query? 回答1: You can try this User.find(:all, limit: 10,

Rails ActiveRecord :joins with LEFT JOIN instead of INNER JOIN

 ̄綄美尐妖づ 提交于 2019-12-17 08:51:57
问题 I have this code User.find(:all, :limit => 10, :joins => :user_points, :select => "users.*, count(user_points.id)", :group => "user_points.user_id") which generates following sql SELECT users.*, count(user_points.id) FROM `users` INNER JOIN `user_points` ON user_points.user_id = users.id GROUP BY user_points.user_id LIMIT 10 is it possible to make LEFT JOIN instead of INNER JOIN other way than User.find_by_sql and manualy typing the query? 回答1: You can try this User.find(:all, limit: 10,

ActiveRecord serialize using JSON instead of YAML

筅森魡賤 提交于 2019-12-17 08:16:34
问题 I have a model that uses a serialized column: class Form < ActiveRecord::Base serialize :options, Hash end Is there a way to make this serialization use JSON instead of YAML? 回答1: In Rails 3.1 you can just class Form < ActiveRecord::Base serialize :column, JSON end Hope that helps 回答2: In Rails 3.1 you can use custom coders with serialize . class ColorCoder # Called to deserialize data to ruby object. def load(data) end # Called to convert from ruby object to serialized data. def dump(obj)

Rails find record with zero has_many records associated [duplicate]

独自空忆成欢 提交于 2019-12-17 08:04:12
问题 This question already has answers here : Want to find records with no associated records in Rails (8 answers) Closed 5 months ago . This seems fairly simple but I can't get it to turn up on Google. If I have: class City < ActiveRecord::Base has_many :photos end class Photo < ActiveRecord::Base belongs_to :city end I want to find all cities that have no photos. I'd love to be able to call something like... City.where( photos.empty? ) ...but that doesn't exist. So, how do you do this kind of

Rails 3 - select with Include?

核能气质少年 提交于 2019-12-17 06:34:11
问题 Here is a nested select with include: @items = Item.where("complete = ?", true).includes( :manufacturer, {:order=>[:supplier, :agent] }) This is a taxing query as it pulls 1000s of rows of data from all the above included tables. How can I get the query to only select specific fields? user.name, user.created_at order.created_at supplier.name agent.name manufacturer.name 回答1: There is a select method in ARel, but you must use the correct table names (i.e. plural and beware if you have

How to execute a raw update sql with dynamic binding in rails

别说谁变了你拦得住时间么 提交于 2019-12-17 06:31:43
问题 I want to execute one update raw sql like below: update table set f1=? where f2=? and f3=? This SQL will be executed by ActiveRecord::Base.connection.execute , but I don't know how to pass the dynamic parameter values into the method. Could someone give me any help on it? 回答1: It doesn't look like the Rails API exposes methods to do this generically. You could try accessing the underlying connection and using it's methods, e.g. for MySQL: st = ActiveRecord::Base.connection.raw_connection

Using helpers in model: how do I include helper dependencies?

梦想的初衷 提交于 2019-12-17 05:45:31
问题 I'm writing a model that handles user input from a text area. Following the advice from http://blog.caboo.se/articles/2008/8/25/sanitize-your-users-html-input, I'm cleaning up the input in the model before saving to database, using the before_validate callback. The relevant parts of my model look like this: include ActionView::Helpers::SanitizeHelper class Post < ActiveRecord::Base { before_validation :clean_input ... protected def clean_input self.input = sanitize(self.input, :tags => %w(b i

query , can not select column count

こ雲淡風輕ζ 提交于 2019-12-17 05:14:22
问题 Tag.joins(:quote_tags).group('quote_tags.tag_id').order('count desc').select('count(tags.id) AS count, tags.id, tags.name') Build query: SELECT count(tags.id) AS count, tags.id, tags.name FROM `tags` INNER JOIN `quote_tags` ON `quote_tags`.`tag_id` = `tags`.`id` GROUP BY quote_tags.tag_id ORDER BY count desc Result: [#<Tag id: 401, name: "different">, ... , #<Tag id: 4, name: "family">] It not return count column for me. How can I get it? 回答1: Have you tried calling the count method on one of

How to query a model based on attribute of another model which belongs to the first model?

佐手、 提交于 2019-12-17 04:34:22
问题 If I have a model Person , which has_many Vehicles and each Vehicle can be of type car or motorcycle , how can I query for all persons, who have cars and all persons, who have motorcycles? I don't think these are correct: Person.joins(:vehicles).where(vehicle_type: 'auto') Person.joins(:vehicles).where(vehicle_type: 'motorcycle') 回答1: You can do as following: Person.includes(:vehicles).where(vehicles: { type: 'auto' }) Person.includes(:vehicles).where(vehicles: { type: 'motorcycle' }) Be

Altering the primary key in Rails to be a string

江枫思渺然 提交于 2019-12-17 04:06:10
问题 So I've got two models, State and Acquisition. State has_many Acquisitions. I felt like an autoincrementing integer primary key for 51 records was rather silly. So I altered the model for the State to be the PK (State being the two letter abbreviation; I'm not storing the actual state name anywhere: class State < ActiveRecord::Base self.primary_key = "state" has_many :acquisition_histories end The problem is when I created my Acquisition model, it created the foreign key column state_id as an