activerecord

Ruby on Rails: Fully functional tableless model

最后都变了- 提交于 2019-12-20 01:05:44
问题 After searching for a tableless model example I came across this code which seems to be the general consensus on how to create one. class Item < ActiveRecord::Base class_inheritable_accessor :columns self.columns = [] def self.column(name, sql_type = nil, default = nil, null = true) columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null) end def all return [] end column :recommendable_type, :string #Other columns, validations and relations etc... end

Matching array values in PostgreSQL with ActiveRecord

北城余情 提交于 2019-12-19 19:44:42
问题 In my Rails 4 app I have a goal to see all contacts, where field visible_to in contacts table equal to 1. My visible_to is :integer, array: true . However, I get the following exception: PG::UndefinedFunction: ERROR: operator does not exist: integer[] = integer LINE 1: ....* FROM "contacts" WHERE "contacts"."visible_to" IN (1) OR... ^ HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.: SELECT "contacts".* FROM "contacts" WHERE "contacts".

Dynamically create query - Rails 5

送分小仙女□ 提交于 2019-12-19 12:01:52
问题 If I manually write a query, it will be like User.where("name LIKE(?) OR desc LIKE(?)",'abc','abc') .where("name LIKE(?) OR desc LIKE(?)",'123','123') However, I need to dynamically generate that query. I am getting data like def generate_query(the_query) query,keywords = the_query # Here # query = "name LIKE(?) OR desc LIKE(?)" # keywords = [['abc','abc'],['123','123']] keywords.each do |keyword| users = User.where(query,*keyword) <-- not sure how to dynamically add more 'where' conditions.

Get the result of SUM and group field from Active record in Codeigniter

回眸只為那壹抹淺笑 提交于 2019-12-19 11:56:45
问题 I need to build this query in Codeigniter but I don't know how to get the result of the SUM: SELECT description, SUM(amount) FROM PAYMENT WHERE (date_payment between '2014-02-01 00:00:00' AND '2014-02-28 23:59:59') GROUP BY description; I'm trying to get the result with this: $qwer = $this->db->query(" SELECT description, SUM(amount) FROM PAYMENT WHERE (date_payment between '$min_date' AND '$max_date') GROUP BY description;"); $i = 0; foreach ($qwer->result() as $payment) { $det_payment[$i] =

building a simple search form in Rails?

喜你入骨 提交于 2019-12-19 10:59:08
问题 I'm trying to build a simple search form in Ruby on Rails, my form is simple enough basically you select fields from a series of options and then all the events matching the fields are shown. The problem comes when I leave any field blank. Here is the code responsible for filtering the parameters Event.joins(:eventdates).joins(:categories).where ("eventdates.start_date = ? AND city = ? AND categories.name = ?", params[:event][:date], params[:event][:city], params[:event][:category]).all From

Codeigniter active record select, left join, count

为君一笑 提交于 2019-12-19 10:53:18
问题 I have a form that shows results from a database query, these results can have many other assets ajoined to them and I wanting to find a way of showing how many assets each elemement has. For example my table is of areas of england an other table has where the users live I current have this code, $this->db->select('*'); $this->db->from('places'); $this->db->join('users, places.place_id = user.place_id, left'); $this->db->get(); The issue I am having is getting the query to return the place

How to map a database view using ActiveRecord?

烂漫一生 提交于 2019-12-19 10:51:55
问题 Has anybody tried mapping database views in oracle using ActiveRecord? Please can I get some sample code for that? 回答1: No code necessary: just use the view name instead of the table in your [ActiveRecord] attribute: [ActiveRecord("MyView")] public class Document {...} Be aware that SchemaExport will treat your view as a table, here's how to fix that. 来源: https://stackoverflow.com/questions/2048483/how-to-map-a-database-view-using-activerecord

Rails has_many :through sum attribute on “child objects” --> SQL Toughy

好久不见. 提交于 2019-12-19 09:07:53
问题 I have three, one-to-many relationships, a has_many :through association and an object with an attribute I'd like to sum. This may sound silly, but assume for example a baseball-themed app: :league has_many :teams :team has_many :users :league has_many :homeruns, :through => :users :user has_many :homeruns What I want to do, on the league show page is list each team in the respective league , and sum how many feet in homeruns each team has, cumulatively. ( Feet is an attribute on homerun .)

rails - using :select(distinct) with :has_many :through association produces invalid SQL

霸气de小男生 提交于 2019-12-19 08:58:28
问题 User has_many :posts has_many :post_tags, :through => :posts PostTag belong_to :post belongs_to :tag scope :distincttag, :select => ('distinct post_tags.tag_id') with Rails 3.0.4, i get invalid SQL: SELECT post_tags.*, distinct tag_id... at least one other person experienced the same problem: http://www.ruby-forum.com/topic/484938 feature or a bug? thanks 回答1: Does not look like the right thing to put on a scope. Maybe you are trying to accomplish this: class PostTag < ... belong_to :post

How to get the current_user in a model observer?

二次信任 提交于 2019-12-19 07:48:49
问题 Given the following models: Room (id, title) RoomMembers (id, room_id) RoomFeed, also an observer When a Room title is updated, I want to create a RoomFeed item, showing who the user is who made the update. @room.update_attributes(:title => "This is my new title") Problem is in my observer for RoomFeed: def after_update(record) # record is the Room object end The is no way for me to get the user.id of the person who just made the update. How do I go about doing that? is there a better way to