activerecord

Extract data from Relations

左心房为你撑大大i 提交于 2019-12-20 04:08:50
问题 I'm making an online shop. I have two models: Product and Category . Product can have one category, while category can have many products. I've defined relationships in models . I can access categories and products. But I want to get all products from specific category. I've tried many examples of relational queries with "lazy" and "eager" approach from official documentation, but no success. Can you please explain how to implement it? Here's my code: Category controller: public function

Extract data from Relations

落花浮王杯 提交于 2019-12-20 04:07:25
问题 I'm making an online shop. I have two models: Product and Category . Product can have one category, while category can have many products. I've defined relationships in models . I can access categories and products. But I want to get all products from specific category. I've tried many examples of relational queries with "lazy" and "eager" approach from official documentation, but no success. Can you please explain how to implement it? Here's my code: Category controller: public function

How to model a many self-referential relationship with many parents?

▼魔方 西西 提交于 2019-12-20 03:49:13
问题 I want to model out the following in the easiest way: A skill has many dependent skills. Each skill should exist on their own, and a skill may have other skills that are prerequisite skills. For example: Skill: Front-End Development Has Dependent Skills -> [HTML, CSS, SCSS] Skill: Web-Design Has Dependent Skills -> [HTML, CSS] Skill: HTML I'd like to be able to do: @front_end_development.dependent_skills ##or a more semantic mapping I'd probably like to walk up the tree but I can't think of

imageable type is not setting for polymorphic associations

荒凉一梦 提交于 2019-12-20 03:18:16
问题 I have two models in my rails 4.2 application class LandingPage < ActiveRecord::Base has_one :section2_photo, -> { where imageable_type: "Section2Photo"}, class_name: Image, foreign_key: :imageable_id, foreign_type: :imageable_type, dependent: :destroy, as: :imageable has_one :section3_photo, -> { where imageable_type: "Section3Photo"}, class_name: Image, foreign_key: :imageable_id, foreign_type: :imageable_type, dependent: :destroy, as: :imageable end and class Image < ActiveRecord::Base

Ruby on Rails: updated_at - set as 0 on creation

别说谁变了你拦得住时间么 提交于 2019-12-20 03:12:18
问题 When I create a new record on query table, I want updated_at (timestamp) to be '0000-00-00 00:00:00' - and not the current timestamp, as Ruby on Rails sets by default. MySQL field: `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP However, Ruby on Rails generates this sql query: INSERT INTO `queries` (`updated_at`) VALUES ('2011-01-04 17:40:15') How do I fix this? 回答1: Rails sets updated_at to the current time because, well, thats when the record was

Get the id of the last updated record

孤者浪人 提交于 2019-12-20 03:03:18
问题 I am able to get the last inserted id using $this->db->insert_id(); in codeigniter, is there any way that I can get the id of the last updated record? I tried it with the same i.e. $this->db->insert_id(); but it doesn't work (returns 0 instead). 回答1: Codeigniter doesn't support that. I had to do this: $updated_id = 0; // get the record that you want to update $this->db->where(array('vrnoa'=>$data['vrnoa'], 'etype' => 'sale')); $query = $this->db->get('StockMain'); // getting the Id $result =

Rails: “Stack level too deep” error when calling “id” primary key method

纵饮孤独 提交于 2019-12-20 02:59:17
问题 This is a repost on another issue, better isolated this time. In my environment.rb file I changed this line: config.time_zone = 'UTC' to this line: config.active_record.default_timezone = :utc Ever since, this call: Category.find(1).subcategories.map(&:id) Fails on "Stack level too deep" error after the second time it is run in the development environment when config.cache_classes = false. If config.cache_classes = true, the problem does not occur. The error is a result of the following code

Ruby autoload conflicts between gmail and parse_resource gems

你说的曾经没有我的故事 提交于 2019-12-20 02:54:56
问题 Earlier, I asked about autoload in the gmail gem not being able to find the files it wanted to load. In building a minimal script, I found the gmail gem loaded it's files when I didn't include the parse_resource gem. The gmail gem lets you access your emails, labels, and inboxes from gmail. The parse_resource gem wraps the parse.com api in an ActiveRecord pattern. If I include the parse_resource gem before the gmail gem, ruby throws a LoadError. These are the permutations of the minimal

Find record, that has ALL associated records

一笑奈何 提交于 2019-12-20 02:49:07
问题 Say, we have a "Person" and "Favorite" models. "Favorite" is what this person likes: "music", "video", "sport", "internet", "traveling" etc. "Person" HABTM "Favorites", and "Favorite" HABTM "Persons" I need to find a Person, that has ALL listed "Favorites. For example, find a person, that likes "music", "traveling" and "sport". How it can be done, using ActiveRecord.find method ? 回答1: @people = Person.find(:all, :joins => :favourites, :select => "person.*, count(favourites) favourite_count",

Array of Hashes with ActiveRecord

一个人想着一个人 提交于 2019-12-20 02:48:08
问题 ActiveRecord (Rails 4.0) supports PostgreSQL Hstore and Array datatypes, so an Array of Hashes is theoretically possible, but my implementation throws: PG::InvalidTextRepresentation: ERROR: malformed array literal: The error is obvious (double quote conflict): "{"null"=>"false","name"=>"schema_id","type"=>"integer","null"=>"false","name"=>"title","type"=>"text"}" : INSERT INTO "entities" ("attribute_hash", "schema_id", "title") VALUES ($1, $2, $3) RETURNING "id" The solution is not obvious to