activerecord

Ruby on Rails - Show field from has_many table

吃可爱长大的小学妹 提交于 2020-01-06 19:33:19
问题 I've got two tables: Ships and Voyages. A ship can have many voyages, but a voyage may only have one ship. In RoR the ship model has a has_many :voyages and the Voyage model is set to belongs_to: ship I can display all of the fields from the ship table in the view without any problems using code similar to this: <%= @ship_data.id %> I'm now trying to show a piece of information from the voyage table in the view. If I do: <%= @ship_data.voyages %> I'm able to pull up the ActiveRecord entry i.e

Update model nested attributes with composite key in rails

情到浓时终转凉″ 提交于 2020-01-06 17:09:33
问题 I have a model with one_to_many relatioship: class Work < ActiveRecord::Base has_many :work_right_holders accepts_nested_attributes_for :work_right_holders, allow_destroy: true end class WorkRightHolder < ActiveRecord::Base self.primary_keys = :work_id, :right_holder_id, :role belongs_to :work belongs_to :right_holder end When I try to update a work with nested attributes, it create new instances of object in the relationship, instead of updating the existing based on their primary key: work

ActiveJob GlobalID and in-memory ActiveRecord objects

两盒软妹~` 提交于 2020-01-06 16:19:12
问题 I'm using a queuing system (Sidekiq) and want to move to ActiveJob to gain a performance benefit of not having to query the database every time I pass an ActiveRecord object to a worker. I wanted to ask and confirm since I wasn't 100% sure but my understanding is that when ActiveJob is using the GlobalID to pass ActiveRecord objects that is all done in memory and a separate query to the database is not done, correct? 回答1: That is not correct. If you use ActiveJob it will serialize any

您是否对数据库项使用源代码管理? [关闭]

╄→尐↘猪︶ㄣ 提交于 2020-01-06 15:28:34
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 我觉得我的商店有一个漏洞,因为我们没有一个可靠的流程来版本化我们的数据库模式更改。 我们做了很多备份,所以我们或多或少都被覆盖了,但以这种方式依赖你的最后一道防线是不好的做法。 令人惊讶的是,这似乎是一个共同点。 我说过的很多商店都忽略了这个问题,因为他们的数据库并没有经常改变,而且他们基本上只是努力做到细致。 但是,我知道这个故事是怎么回事。 事情排列错误只是一个时间问题而且缺少某些东西。 对此有什么最佳做法吗? 有哪些策略对你有用? #1楼 我相信每个数据库都应该受源代码控制,开发人员应该有一个简单的方法从头开始创建他们的本地数据库。 受Visual Studio for Database Professionals的启发,我创建了一个脚本MS SQL数据库的开源工具,并提供了将它们部署到本地数据库引擎的简便方法。 试试 http://dbsourcetools.codeplex.com/ 。 玩得开心, - 弥敦道。 #2楼 数据库本身? 没有 创建它们的脚本,包括静态数据插入,存储过程等; 当然。 它们是文本文件,它们包含在项目中,并像其他所有内容一样进行检入和检出。 当然,在理想的世界中,您的数据库管理工具会这样做; 但你必须遵守纪律。 #3楼 我通过保存创建/更新脚本和生成采样数据的脚本来实现。

devise : new user registration issues , PG::ProtocolViolation . Rails4, Postgres

Deadly 提交于 2020-01-06 15:13:39
问题 I am using devise and omni-auth to login with linkedin . I added the from_omniauth method to user model as mentioned in the Railscast: This is the code: #Create a new user if does not exist def self.from_omniauth(auth) where(auth.slice(:provider, :uid)).first_or_create! do |user| user.provider = auth.provider user.uid = auth.uid user.email = auth.info.email end end Started GET "/users/auth/linkedin/callback?oauth_token=XXX" INFO -- omniauth: (linkedin) Callback phase initiated. Processing by

Sum of ingredients in recipes model to create shoppinglist

走远了吗. 提交于 2020-01-06 14:45:25
问题 I Have a shoppinglist model which has many recipes, which again has many ingredients. class Shoppinglist < ActiveRecord::Base has_many :shoppinglistrecipezations has_many :recipes, :through => :shoppinglistrecipezations end class Recipe < ActiveRecord::Base has_many :shoppinglistrecipezations has_many :shoppinglists, :through => :shoppinglistrecipezations has_many :ingredients, :through => :ingredienzations has_many :ingredienzations end class Ingredient < ActiveRecord::Base has_many :recipes

ActiveRecord OR clause in scoped query

与世无争的帅哥 提交于 2020-01-06 14:19:40
问题 statuses = %w(sick healthy hungry) query = User.scoped(:joins => 'left outer join pets on pets.user_id = users.id', :conditions => { 'pets.id' => nil, 'users.job_status' => stati }) Given the code above, is it possible to add an OR clause to the :conditions to say something like where (pets.id is NULL AND users.status IN ('sick', 'healthy', 'hungry')) OR (users.gender = 'male') 回答1: You can do the following using the MetaWhere gem statuses = %w(sick healthy hungry) query = User.include(:pets)

Ruby 1.92 in Rails 3: A Case where Array.length Does Not Equal Array.count?

拜拜、爱过 提交于 2020-01-06 14:17:14
问题 My understanding is that count and length should return the same number for Ruby arrays. So I can't figure out what is going on here (FactoryGirl is set to create--save to database--by default): f = Factory(:family) # Also creates one dependent member f.members.count # => 1 f.members.length # => 1 m = Factory(:member, :family=>f, :first_name=>'Sam') #Create a 2nd family member f.members.count # => 2 f.members.length # => 1 puts f.members # prints a single member, the one created in the first

Creating a form with unknown fields and storing those fields into a serialized filed in my model

寵の児 提交于 2020-01-06 12:42:26
问题 I have a form that will have both a dynamic set and a known set of fields. I need a way of storing the dynamic fields in the database and I have decided on storing them in a serialized field, as I will not need to search on the data, and I just need it stored and recalled when needed. class MyApplication < ActiveRecord::Base has_one :applicant belongs_to :member serialize :additional_fields, Hash accepts_nested_attributes_for :applicant, :additional_fields I was thinking of having the form

RnR: Database normalization, rails models and associations

末鹿安然 提交于 2020-01-06 12:23:41
问题 Ok, so I have three different objects: a person, place, and equipment. Each can have an address, in most cases multiple address and multiple phone numbers. So my thought is create the specific object tables, the have an address and a phone table. Here my question: So typically, in my SQL world, I would just have an object_id column in the address and phone table and put a id of the object in the object id column, and then select all the address or phone records that match. I could do this, do