activerecord

Rails include only selected columns from relation

二次信任 提交于 2021-02-06 10:22:44
问题 I will try to explain: I have a table 'Product' and a table 'Store'. I'm trying to get just the store name to show at the page, but ActiveRecord is returning me all the columns from the store. Here is the code: @product = Product .order(id: :desc) .includes(:store) .select("products.id, products.store_id, stores.name") .limit(1) The built query (from Rails): Processing by IndexController#index as HTML Product Load (0.0ms) SELECT products.id, products.store_id FROM `products` ORDER BY

Rails include only selected columns from relation

被刻印的时光 ゝ 提交于 2021-02-06 10:21:56
问题 I will try to explain: I have a table 'Product' and a table 'Store'. I'm trying to get just the store name to show at the page, but ActiveRecord is returning me all the columns from the store. Here is the code: @product = Product .order(id: :desc) .includes(:store) .select("products.id, products.store_id, stores.name") .limit(1) The built query (from Rails): Processing by IndexController#index as HTML Product Load (0.0ms) SELECT products.id, products.store_id FROM `products` ORDER BY

Rails include only selected columns from relation

蓝咒 提交于 2021-02-06 10:21:30
问题 I will try to explain: I have a table 'Product' and a table 'Store'. I'm trying to get just the store name to show at the page, but ActiveRecord is returning me all the columns from the store. Here is the code: @product = Product .order(id: :desc) .includes(:store) .select("products.id, products.store_id, stores.name") .limit(1) The built query (from Rails): Processing by IndexController#index as HTML Product Load (0.0ms) SELECT products.id, products.store_id FROM `products` ORDER BY

Rendering a JSON object of a join-model and its associated models

喜欢而已 提交于 2021-02-06 09:04:28
问题 In a Rails ( 4.1.5 / ruby 2.0.0p481 / win64 ) application I have a many-to-many relationship between Student and Course and a join model StudentCourse which represents the association, and has an additional attribute called started (set by default on "false"). I also have added an index in the join-table made of student_id and course_id , and set a unique check on that, like this t.index [:student_id, :course_id], :unique => true, :name => 'by_student_and_course' I wanted that to be a

Rendering a JSON object of a join-model and its associated models

蹲街弑〆低调 提交于 2021-02-06 09:03:28
问题 In a Rails ( 4.1.5 / ruby 2.0.0p481 / win64 ) application I have a many-to-many relationship between Student and Course and a join model StudentCourse which represents the association, and has an additional attribute called started (set by default on "false"). I also have added an index in the join-table made of student_id and course_id , and set a unique check on that, like this t.index [:student_id, :course_id], :unique => true, :name => 'by_student_and_course' I wanted that to be a

Understanding Rails ActiveRecord “single model” self joins

让人想犯罪 __ 提交于 2021-02-05 13:28:14
问题 I'm having a hard time understanding how to implement a single model self-join in Rails. The Guide to ActiveRecord Associations section 2.10 briefly explains Self-Joins but doesn't offer enough information, and every example or post about this such references the Friendly Friend Railcast example that isn't a single model self join, as described in the Rails Guide section 2.10. The idea is a model that has_many and belongs_to itself, without needing a separate table for the relationship. The

Understanding Rails ActiveRecord “single model” self joins

烈酒焚心 提交于 2021-02-05 13:26:38
问题 I'm having a hard time understanding how to implement a single model self-join in Rails. The Guide to ActiveRecord Associations section 2.10 briefly explains Self-Joins but doesn't offer enough information, and every example or post about this such references the Friendly Friend Railcast example that isn't a single model self join, as described in the Rails Guide section 2.10. The idea is a model that has_many and belongs_to itself, without needing a separate table for the relationship. The

Understanding Rails ActiveRecord “single model” self joins

只愿长相守 提交于 2021-02-05 13:25:20
问题 I'm having a hard time understanding how to implement a single model self-join in Rails. The Guide to ActiveRecord Associations section 2.10 briefly explains Self-Joins but doesn't offer enough information, and every example or post about this such references the Friendly Friend Railcast example that isn't a single model self join, as described in the Rails Guide section 2.10. The idea is a model that has_many and belongs_to itself, without needing a separate table for the relationship. The

Find records in deeply nested associations with rails

▼魔方 西西 提交于 2021-02-05 07:14:25
问题 I have the following models: class Book < ApplicationRecord has_many :chapters end class Chapter < ApplicationRecord belongs_to :book has_many :pages end class Page < ApplicationRecord belongs_to :chapter has_many :paragraphs end class Paragrpah < ApplicationRecord belongs_to :page end Now I want to get a list of all paragraphs in a specific book. Something like: @parapgraphs = Paragraph.pages.chapters.book.where(:title => 'My Book') When I do this, I get: undefined method 'chapters' for

Codeigniter, increase database value by value contained in variable

可紊 提交于 2021-02-02 09:16:37
问题 I am using codeigniter, and I have the following function in my model to give points to the user. It is however not working, rather setting the points column to 0. This is how it is written in the codeigniter manual. Therefore I have no clue why it is not working... Thanks function give_points($username,$points) { $this->db->set('points', 'points + $points'); $this->db->where('username', $username); $this->db->update('users'); echo"done"; } 回答1: I believe you have to tell CI specifically to