activerecord

Mysql Codeigniter Active Record - How do I do a where_in query and return the correct order of results?

本秂侑毒 提交于 2019-12-09 18:29:38
问题 I have a set of IDs passed in a particular order which needs to be retained. I am querying from several left joins the data related to each ID. The IDs are returned from a search so the order must be kept to make the results valid (otherwise it makes searching rather pointless). My code looks something like; $this->db->where_in('id', $array_of_ordered_ids); For example - $this->db->where_in('id', array(4,5,2,6)); Will return the results in order 2,4,5,6. I'd like for it to retain the order

How do I create an ActiveRecord relationship to an ActiveResource object?

China☆狼群 提交于 2019-12-09 18:24:09
问题 Let's say I'm writing a Library application for a publishing company who already has a People application. So in my Library application I have class Person < ActiveResource::Base self.site = "http://api.people.mypublisher.com/" end and now I want to store Article s for each Person : class Article < ActiveRecord::Base belongs_to :person, :as => :author end I imagine I'd have the following table in my database: Articles id (PK) | title (string) | body (text) | author_id (integer) author_id isn

Ruby/Rails - Access a “lookup” table without modeling it?

时光毁灭记忆、已成空白 提交于 2019-12-09 17:58:20
问题 This is for a Ruby on Rails 3.0.x project. I have a "lookup" table with data from a vendor. When I import data from another source, I want to check this table (join on a SKU) for extra data. It doesn't seem right to me to create a model for this table in my app. The data will never be changed by my app, and it doesn't need to have any model associations beyond the data lookup I just mentioned. It's just occasionally accessed to check for some info. What is the best practice to access this

Rails 4 HABTM custom validation on associations

空扰寡人 提交于 2019-12-09 17:41:16
问题 I've got a simple scenario, but I can't seem to find any proposed solutions that apply to Rails 4. I want to simply add a custom validator that checks the amount of stored associations between my HABTM association. Easier said and done, to my surprise? I've searched for a solution but only end up with answers for older versions of Rails it seems. I've got the following: class User < ActiveRecord::Base has_and_belongs_to_many :roles after_save :check_maximum_number_of_roles . . . private def

Why am I getting this “undefined method `#<ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_Survey” error?

落爺英雄遲暮 提交于 2019-12-09 17:38:51
问题 I have the following associations: class Question < ActiveRecord::Base has_and_belongs_to_many :footnotes has_and_belongs_to_many :pictures has_many :fields, :dependent => :destroy has_many :surveys, :dependent => :delete_all belongs_to :input belongs_to :element has_many :screenshots belongs_to :standard, :touch => true belongs_to :product, :touch => true belongs_to :condition, :class_name => "Field", :touch => true end class Product < ActiveRecord::Base has_many :questions, :dependent =>

Dynamically create after_add and after_remove callbacks for has_many or habtm?

前提是你 提交于 2019-12-09 17:25:40
问题 Is there a way to dynamically add after_add and after_remove callbacks to an existing has_many or has_and_belongs_to_many relationship? For example, suppose I have models User , Thing , and a join model UserThingRelationship , and the User model is something like this: class User < ActiveRecord::Base has_many :user_thing_relationships has_many :things, :through => :user_thing_relationships end I'd like to be able to, in a module that extends User , add :after_add and :after_remove callbacks

rails scope and joins

廉价感情. 提交于 2019-12-09 17:25:37
问题 I have tried everything i thought would work for this and am turning up nothing. in rails 3, I need to find all users with a cd player in their car. A car has one user and one radio, and a user belongs to a car, and a radio has many cars. I am stumbling on how I would perform this search via a scope in the user model. class User belongs_to :car class Car belongs_to radio has_one :user, :dependent => destroy class Radio has_many :cars 回答1: I am assuming that you mean this: Car has radio_id ,

Rails creating schema_migrations - Mysql2::Error: Specified key was too long

烈酒焚心 提交于 2019-12-09 17:21:48
问题 I am using Rails 3.2.6 and Mysql 6.0.9 (but I have exactly the same error on MySQL 5.2.25) When I create new database ( rake db:create ) and then when I try to load the schema ( rake schema:load ) I get this error: Mysql2::Error: Specified key was too long; max key length is 767 bytes: CREATE UNIQUE INDEX `unique_schema_migrations` ON `schema_migrations` (`version`) After hours and hours of research I found these solutions: 1. Change MySQL variable innodb_large_prefix to true (or ON) This

what is the equivalent of “find_all_by_id” in rails 4

大城市里の小女人 提交于 2019-12-09 17:08:42
问题 I have an array of ids that I want to find their respective records from the database, using active record query. e.g: ids = [2, 3, 1] now, for me to find all records of a particular model whose id is one of those in the array, In lower versions of rails, I guess I can do something like: Model.find_all_by_id([2, 3, 1]) but according to this post, These methods were deprecated on 4.0 and removed at 4.1. If you want to keep sing then you need to include this gem https://github.com/rails

rails find: using conditions while including same table twice via different named associations

微笑、不失礼 提交于 2019-12-09 16:50:31
问题 I have posts which are sent by users to other users. There are two models - :post and :user, and :post has the following named associations: belongs_to :from_user, :class_name => "User", :foreign_key => "from_user_id" belongs_to :to_user, :class_name => "User", :foreign_key => "to_user_id" Both :user and :post have "is_public" column, indicating that either a single post and/or the entire user profile can be public or private. My goal is to fetch a list of posts which are public AND whose