has-many

Why in the world would I have_many relationships?

十年热恋 提交于 2019-12-08 16:54:02
问题 I just ran into an interesting situation about relationships and databases. I am writing a ruby app and for my database I am using postgresql. I have a parent object "user" and a related object "thingies" where a user can have one or more thingies. What would be the advantage of using a separate table vs just embedding data within a field in the parent table? Example from ActiveRecord: using a related table: def change create_table :users do |i| i.text :name end create_table :thingies do |i|

How to get highest count of associated model (Rails)?

人走茶凉 提交于 2019-12-08 16:18:15
问题 A User has_many Solutions How do I order Users by those with the most Solutions? I'm trying to find the top ten users but I'm not sure how the most tidy or efficient way to do this? Does anyone have an example that isn't too computationally expensive? 回答1: If you really want a fast way of doing it, put a counter_cache on a users' solutions (have a solutions_count column in your User ) and order by that column. You don't need to manage that counter, because rails does it for you. You can read

How to get relationship counts without loading objects in laravel

守給你的承諾、 提交于 2019-12-08 10:44:15
问题 I have a model customer and it has many projects. I want to find projects count without including its object. Customer model includes: public function numberOfProjects() { return $this->hasMany(Project::class)->count(); } Query in my controller: $customers = Customer::where(['is_active'=>1]) ->with(['customerContactInformation'=> function ($query) { $query->where('is_active',1); }, 'numberOfProjects']) ->skip($skip)->take(10) ->get(); Its giving me error:Call to a member function

Rails associations has_many through ban/archive solution?

半腔热情 提交于 2019-12-08 06:40:22
问题 I'm new in Rails and am working on a problem. I have two tables: Shoes and Socks A Shoe can have many Socks, but only one active Sock. Other Socks are inactive. All Socks are also unique with unique patterns. I want to make sure I don't have socks with the same pattern. I think I can do this three ways 1) Using an additional column in table socks to represent the active sock class Shoe < ActiveRecord::Base has_many :socks end class Sock < ActiveRecord::Base belongs_to :shoe end class

rails: how to update a has_many :through relation via jQuery?

本小妞迷上赌 提交于 2019-12-08 03:43:17
问题 Sorry, if this is a noobish question, but I'm just getting started with Rails and jQuery. I have the following scenario: I have 3 classes: contacts , companies and contact_company_joins (ccj). For all three classes I created models , controller and views . Obviously, contacts and companies are connected via the join-table ccj (using has_many :through ). contacts <-> contact_company_joins <-> companies In my contact's "show" view I want to display a table that lists all companies that the

Weird relationship behavior on Rails 3 and update_attribute

六眼飞鱼酱① 提交于 2019-12-08 03:22:10
问题 I'm having a hard time trying to find out why a test is failing: describe User, "Instance Methods" do describe "leave_group!" do it "should set group_id to nil" do @user = Factory(:user) @group_2 = Factory(:group, :owner => @user) @user.leave_group! @user.reload.group_id.should be_nil # THIS LINE IS FAILING!!! end it "should assign another owner to group, if owner was user" do @user = Factory(:user) @group = Factory(:group, :owner => @user) 1.upto(4) { @group.add_user Factory(:user) } @user

rails 4 has_many :through not saving associations

旧街凉风 提交于 2019-12-07 16:10:20
问题 I have 2 models with many to many association as follows: class User < ActiveRecord::Base has_many :remark_users, :dependent => :destroy has_many :designated_remarks, :through => :remark_users, :source => :remark end class Remark < ActiveRecord::Base has_many :remark_users, :dependent => :destroy has_many :users, :through => :remark_users accepts_nested_attributes_for :users end And the relationship: class RemarkUser < ActiveRecord::Base belongs_to :remark belongs_to :user end The remarks

Rails - how to store “has_many” checkboxes association in database?

天涯浪子 提交于 2019-12-07 08:27:39
问题 I have the table User and the table Categories . The relation between these two tables: user has_many :categories category belongs_to :user I would like to display on the user's (standard) edit page the list of categories with checkboxes. When this user would check some of the checkboxes, I would like to save them and then display as checked when he open the page the next time. But how to render the checkboxes in the view? And how to store information about what checkboxes the user checked in

ActiveRecord Associations: Any gotchas if has_many WITHOUT corresponding belongs_to?

爱⌒轻易说出口 提交于 2019-12-07 06:35:18
问题 A phone has many messages. An email address has many messages. A message either belongs to a phone, email, or neither. The belongs_to association is optional. The following associations seem to work fine for these relationships: Phone model has_many :messages Email model has_many :messages Message model does NOT have belongs_to :phones, :email Is this okay or is there some proper way to specify a "can_belong_to" relationship? 回答1: It is completely correct unidirectional relation. Using both

ActiveRecord has_many where two columns in table A are primary keys in table B

故事扮演 提交于 2019-12-07 02:27:06
问题 I have a model, Couple , which has two columns, first_person_id and second_person_id and another model, Person , whose primary key is person_id and has the column name Here's the usage I want: #including 'Person' model for eager loading, this is crucial for me c = Couple.find(:all, :include => :persons)[0] puts "#{c.first_person.name} and #{c.second_person.name}" So how can I do this? 回答1: The relationships declared in Couple should look like this: class Couple named_scope :with_people, {