has-many

Weird relationship behavior on Rails 3 and update_attribute

我是研究僧i 提交于 2019-12-06 15:58:22
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.leave_group! @group.reload.owner.should_not eql(@user) end end end These are the models I'm using: class

Access extra attributes in join table when using though in has_many relationship

旧街凉风 提交于 2019-12-06 13:21:33
问题 I was considering adding some extra properties between a has_many relationship. For example, I have a Users table, and a Group table. Users can join groups via a :through has_many relationship. I want to add the property of the 'role' of the user in that group. create_table "groupization", :force => true do |t| t.integer "user_id" t.integer "group_id" t.string "role" t.datetime "created_at", :null => false t.datetime "updated_at", :null => false end I was wondering, how can I access the role

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

假如想象 提交于 2019-12-06 10:56:04
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 contact is connected to (and some additional info on the company itself e.g. phone-number). In my company's

When are Active Record objects in has_many relationships saved?

牧云@^-^@ 提交于 2019-12-06 04:44:43
问题 I'm using Rails 1.2.3 (yeah, I know) and am confused about how has_many works with respect to object persistence. For the sake of example, I'll use this as my declaration: class User < ActiveRecord::Base has_many :assignments end class Assignment < ActiveRecord::Base belongs_to :user end As I understand it, this generates, among others, a method User#assignments.build , which creates an Assignment object whose user_id is the receiving instance's id (and whose other fields are as specified in

rails 4 has_many :through not saving associations

我只是一个虾纸丫 提交于 2019-12-06 03:29:13
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_controller action that should do the save: # PATCH Save users def save_users @remark = Remark.find(params[

Error while using `find_or_create_by` on a `has_many` `through` association

两盒软妹~` 提交于 2019-12-06 01:52:29
问题 I am running in to a problem while using find_or_create_by on a has_many through association. class Permission < ActiveRecord::Base belongs_to :user belongs_to :role end class Role < ActiveRecord::Base # DB columns: user_id, role_id has_many :permissions has_many :users, :through => :permissions end class User has_many :permissions has_many :roles, :through => :permissions end Rails throws an error when I invoke find_or_create_by on roles association of a User object. u = User.first u.roles

EmberJS: Unable to get the length of an hasMany array two levels down

烈酒焚心 提交于 2019-12-05 22:24:18
问题 Im trying to create a computed property to get me the sum of the length of all pages. But i cannot figure out how to access a child so i can get the childs of that child. App.Document = DS.Model.extend({ name: DS.attr('string'), spreads: DS.hasMany('App.Spread'), pagesCount: function() { // Here is where i go wrong, i can get the length of spreads, but not access a spread to get the page length. var spreadsLength = this.get('spreads.length'); var firstSpread = this.get('spreads')[0]; return

ruby on rails - how to make relationship works in route, controller, view ? has_many, belongs_to

…衆ロ難τιáo~ 提交于 2019-12-05 12:14:39
I am struggling to get my relationship in rails to work. I have a User,Gallery,Comment model class Gallery has_many :comments belongs_to :user end class User has_many :comments has_many :galleries end class Comment belongs_to :gallery belongs_to :user end now what should i do in the routes, controller and views to link this all up ? please help me ? its quite confusing finding out the answers. If can, I dont want it to be nested like in the railscast, but i want for each model, eg gallery i can input user, eg comment i can find and input the galleryid and userid. Im totally lost in oblivion

Is it possible to have a compound foreign key in rails?

和自甴很熟 提交于 2019-12-05 09:10:00
Suppose the following data schema: Usage ====== client_id resource type amount Billing ====== client_id usage_resource usage_type rate In this example, suppose I have multiple resources, each of which can be used in many ways. For example, one resource is a widget . Widgets can be foo ed and they can be bar ed. Gizmo s can also be foo ed and bar ed. These usage types are billed at different rates, possibly even different rates for different clients. Each occurence of a usage (of a resource) is recorded in the Usage table. Each billing rate (for client, resource, and type combination) is stored

Create association between two instancied objects

允我心安 提交于 2019-12-05 03:43:36
I have two models: (Albums and Product) 1) Inside Models Inside album.rb: class Album < ActiveRecord::Base attr_accessible :name has_many :products end Inside product.rb: class Product < ActiveRecord::Base attr_accessible :img, :name, :price, :quantity belongs_to :album end 2) Using " rails console ", how can I set the associations (so I can use "<%= Product.first.album.name %>")? e.g. a = Album.create( :name => "My Album" ) p = Product.create( :name => "Shampoo X" ) # what's next? how can i set the album and the product together? You can do like this: a = Album.create( name: "My Album" ) p =