Has_many :through reversed access? - ruby-on-rails

Not exactly an error, but I think I am missing something important here..
class Team < ActiveRecord::Base
has_many :groups
has_many :users, :through => :groups
class User < ActiveRecord::Base
acts_as_authentic
has_many :groups
has_many :teams, :through => :groups
class Group < ActiveRecord::Base
belongs_to :user
belongs_to :team
So I can do something like:
user_test.teams << team_test
and I expect that after that I should be able to do something like:
team_test.users
it will list user_test among all others.. But it does not..
What am I missing?
Thanks!
EDIT::
ruby-1.9.3-p0 :001 > user_test = User.create
(0.0ms) SAVEPOINT active_record_1
(0.1ms) SELECT 1 FROM "users" WHERE "users"."persistence_token" = '6f2890df599776198476630fad3db57b62606339d7ec2c1e96cc4081919789fa0a7cac5ffaed6b8f61f28f3ff2abd6ca890eb623c1b2d6718328d10527fa1566' LIMIT 1
(0.0ms) ROLLBACK TO SAVEPOINT active_record_1
=> #<User id: nil, username: nil, email: nil, crypted_password: nil, password_salt: nil, persistence_token: "6f2890df599776198476630fad3db57b62606339d7ec2c1e96c...", created_at: nil, updated_at: nil>
ruby-1.9.3-p0 :002 > team_test = Team.create
(0.0ms) SAVEPOINT active_record_1
SQL (1.9ms) INSERT INTO "teams" ("created_at", "name", "personal", "project_id", "updated_at", "visible") VALUES (?, ?, ?, ?, ?, ?) [["created_at", Tue, 22 Nov 2011 23:09:28 UTC +00:00], ["name", nil], ["personal", false], ["project_id", nil], ["updated_at", Tue, 22 Nov 2011 23:09:28 UTC +00:00], ["visible", nil]]
(0.0ms) RELEASE SAVEPOINT active_record_1
=> #<Team id: 8, name: nil, created_at: "2011-11-22 23:09:28", updated_at: "2011-11-22 23:09:28", visible: nil, personal: false, project_id: nil>
ruby-1.9.3-p0 :003 > user_test.teams << team_test
(0.1ms) SAVEPOINT active_record_1
(0.0ms) RELEASE SAVEPOINT active_record_1
=> [#<Team id: 8, name: nil, created_at: "2011-11-22 23:09:28", updated_at: "2011-11-22 23:09:28", visible: nil, personal: false, project_id: nil>]
ruby-1.9.3-p0 :004 > user_test.teams
=> [#<Team id: 8, name: nil, created_at: "2011-11-22 23:09:28", updated_at: "2011-11-22 23:09:28", visible: nil, personal: false, project_id: nil>]
ruby-1.9.3-p0 :005 > team_test.users
User Load (0.1ms) SELECT "users".* FROM "users" INNER JOIN "groups" ON "users"."id" = "groups"."user_id" WHERE "groups"."team_id" = 8
=> []

This is strange... Are user_test and team_test saved or only initialized?
1/ the both are saved:
user_test = User.create
team_test = Team.create
user_test.teams << team_test
user_test.teams # => team_test among others
team_test.users # => user_test among others
2/ only one is saved:
a) the saved model is the one which 'received' the other:
user = User.create
team = Team.new
user.teams << team
# team is saved automatically
user.teams # => team among others
team.users # => user among others (because team was saved automatically)
b) the saved model is the one which 'is received' by the other:
user = User.new
team = Team.create
user.teams << team
user.teams # => return team
team.users # => [] (empty array; the 'receiver' is not saved automatically)
3/ none is saved
user = User.new
team = Team.new
users.teams << team
user.teams # => team but not saved (i.e. id is nil)
team.users # => [] (empty array)
You might be in case 2.b or in case 3.

Related

Method find_by_user_id_and_friend_id does not seem to work - Rails 5

i am unsure why the method
Friendship.find_by_user_id_and_friend_id(user, friend) &
Friendship.find_by_user_id_and_friend_id(friend, user) gives a nil,
could one kindly explain this to me
why does the method Friendship.find_by_user_id_and_friend_id(user,
friend) give a nil and the method
Friendship.find_by_user_id_and_friend_id(user.id, friend.id) does
not? basically could one explain the difference to me
2.3.0 :065 > user
=> #<User id: 1, email: "richill#gmail.com", created_at: "2016-11-22 15:56:19", updated_at: "2016-12-06 11:39:29", firstname: "richill", lastname: "artloe">
2.3.0 :068 > friend
=> #<User id: 2, email: "emma#gmail.com", created_at: "2016-11-22 16:19:25", updated_at: "2016-11-22 16:19:25", firstname: "emma", lastname: "watson">
Friendship.create(user: user, friend: friend, status: 'pending')
Friendship.create(user: friend, friend: user, status: 'requested')
2.3.0 :078 > Friendship.find_by_user_id_and_friend_id(user, friend)
Friendship Load (0.2ms) SELECT "friendships".* FROM "friendships" WHERE "friendships"."user_id" = ? AND "friendships"."friend_id" = ? LIMIT ? [["user_id", nil], ["friend_id", nil], ["LIMIT", 1]]
=> nil
2.3.0 :079 >
2.3.0 :079 > Friendship.find_by_user_id_and_friend_id(friend, user)
Friendship Load (0.2ms) SELECT "friendships".* FROM "friendships" WHERE "friendships"."user_id" = ? AND "friendships"."friend_id" = ? LIMIT ? [["user_id", nil], ["friend_id", nil], ["LIMIT", 1]]
=> nil
2.3.0 :080 >
i believe i am suppose to get something like the below results:
>> Friendship.find_by_user_id_and_friend_id(user, friend)
=> #<Friendship:0x2bf74ec #attributes={"status"=>"pending", "accepted_at"=>nil,
"id"=>"1", "user_id"=>"1", "position"=>nil, "created_at"=>"2007-01-03 18:34:09",
"friend_id"=>"1198"}>
>> Friendship.find_by_user_id_and_friend_id(friend, user)
=> #<Friendship:0x490a7a0 #attributes={"status"=>"requested", "accepted_at"=>nil
, "id"=>"2", "user_id"=>"1198", "position"=>nil, "created_at"=>"2007-01-03 18:34
:20", "friend_id"=>"1"}>
very strange. it works when placed like this
Friendship.find_by_user_id_and_friend_id(user.id, friend.id)
=> #<Friendship id: 1, user_id: 1, friend_id: 2, status: "pending", created_at: "2016-12-06 11:55:06", updated_at: "2016-12-06 11:55:06">
but does not work when placed like this:
Friendship.find_by_user_id_and_friend_id(user, friend)
=> nil
could one explain why this is to me
what is the difference
2.3.0 :016 > Friendship.find_by_user_id_and_friend_id(user, friend)
Friendship Load (0.2ms) SELECT "friendships".* FROM "friendships" WHERE "friendships"."user_id" = ? AND "friendships"."friend_id" = ? LIMIT ? [["user_id", nil], ["friend_id", nil], ["LIMIT", 1]]
=> nil
2.3.0 :017 > Friendship.find_by_user_id_and_friend_id(user.id, friend.id)
Friendship Load (0.2ms) SELECT "friendships".* FROM "friendships" WHERE "friendships"."user_id" = ? AND "friendships"."friend_id" = ? LIMIT ? [["user_id", 1], ["friend_id", 2], ["LIMIT", 1]]
=> #<Friendship id: 1, user_id: 1, friend_id: 2, status: "pending", created_at: "2016-12-06 11:55:06", updated_at: "2016-12-06 11:55:06">
2.3.0 :018 >
the find_by_id method is the same as using the where method except it only returns the first record that is found. (See the source code)
Since the method is searching for id's I believe you need to pass the id directly rather than the instance of your object for it to work. This is the same when you use the .find method.

Updating Rails 4 Nested Attributes

I'm having a hard time with Rails and nested attributes and would really appreciate some help.
Here is the output from my console session where I was attempting to get the updated values to save but as you can see, they don't seem to take on the next line when I perform the find again:
irb(main):070:0* e = Equipment.find(26)
Equipment Load (0.5ms) SELECT "equipment".* FROM "equipment" WHERE "equipment"."id" = $1 LIMIT 1 [["id", 26]]
=> #<Equipment id: 26, name: "fdsfsdsdfsd2", created_at: "2015-11-02 15:26:43", updated_at: "2015-11-02 16:38:55", site_id: 57, type_id: 3>
irb(main):071:0> e.update({"name"=>"fdsfsdsdfsd2", "site_id"=>"57", "type_id"=>"3", "equipment_properties_attributes"=>{"0"=>{"id"=>"15", "value"=>"2015-10-34", "property_id"=>"4"}, "1"=>{"id"=>"16", "value"=>"fsdfdsfsd", "property_id"=>"5"}}})
(0.6ms) BEGIN
EquipmentProperty Load (0.7ms) SELECT "equipment_properties".* FROM "equipment_properties" WHERE "equipment_properties"."equipment_id" = $1 AND "equipment_properties"."id" IN (15, 16) [["equipment_id", 26]]
(0.2ms) COMMIT
=> true
irb(main):072:0> e.equipment_properties
EquipmentProperty Load (0.5ms) SELECT "equipment_properties".* FROM "equipment_properties" WHERE "equipment_properties"."equipment_id" = $1 [["equipment_id", 26]]
=> #<ActiveRecord::Associations::CollectionProxy [#<EquipmentProperty id: 15, equipment_id: 26, property_id: 4, value: "2015-10-34", created_at: "2015-11-02 15:26:51", updated_at: "2015-11-02 15:26:51">, #<EquipmentProperty id: 16, equipment_id: 26, property_id: 5, value: "fsdfdsfsd", created_at: "2015-11-02 15:26:51", updated_at: "2015-11-02 15:26:51">]>
irb(main):073:0> e = Equipment.find(26)
Equipment Load (0.5ms) SELECT "equipment".* FROM "equipment" WHERE "equipment"."id" = $1 LIMIT 1 [["id", 26]]
=> #<Equipment id: 26, name: "fdsfsdsdfsd2", created_at: "2015-11-02 15:26:43", updated_at: "2015-11-02 16:38:55", site_id: 57, type_id: 3>
irb(main):074:0> e.equipment_properties
EquipmentProperty Load (0.6ms) SELECT "equipment_properties".* FROM "equipment_properties" WHERE "equipment_properties"."equipment_id" = $1 [["equipment_id", 26]]
=> #<ActiveRecord::Associations::CollectionProxy [#<EquipmentProperty id: 15, equipment_id: 26, property_id: 4, value: "2015-10-30", created_at: "2015-11-02 15:26:51", updated_at: "2015-11-02 15:26:51">, #<EquipmentProperty id: 16, equipment_id: 26, property_id: 5, value: "fsdfdsfsd", created_at: "2015-11-02 15:26:51", updated_at: "2015-11-02 15:26:51">]>
The same thing is happening with the web interface. I can provide additional details if anyone needs them but I am allowing the parameters through and on creation, the initial values are saved.
I've been beating my head against this all morning and I suspect it is something stupid but I'm just not sure what to try next. Thanks!
UPDATE 1:
Equipment Model:
class Equipment < ActiveRecord::Base
belongs_to :site
belongs_to :type
has_and_belongs_to_many :properties
has_many :equipment_properties
accepts_nested_attributes_for :equipment_properties, reject_if: :all_blank, allow_destroy: true
end
And also the equipment_properties model:
class EquipmentProperty < ActiveRecord::Base
belongs_to :equipment
belongs_to :property
has_one :type, through: :equipment
end
Also, of relevance might be that I can update the individual equipment_property without nesting and that does work.
UPDATE 2:
I managed to add this to the controller and it saves the values now. Not pretty but it works I guess...
equipment_params[:equipment_properties_attributes].each do |property|
ep = EquipmentProperty.where(id: property[1][:id]).first
#logger.debug "EP Value: #{ep.value}"
#logger.debug "Property Value: #{property[1][:value]}"
ep.value = property[1][:value]
ep.save
end
This is what I ended up adding to the controller to resolve this. Definitely a hack though and I'm not sure why the updates are taking:
equipment_params[:equipment_properties_attributes].each do |property|
ep = EquipmentProperty.where(id: property[1][:id]).first
#logger.debug "EP Value: #{ep.value}"
#logger.debug "Property Value: #{property[1][:value]}"
ep.value = property[1][:value]
ep.save
end

Rails Updating attribute always inserts nil

Having an issue with a database update. At first I thought it might be related to Carrierwave but after this test I don't think so anymore.
Here I try and update the attribute in the rails console, only to have the image field return nil. But notice that the property_id field changes.
2.2.0 :004 > Propimage
=> Propimage(id: integer, property_id: integer, image: string, created_at: datetime, updated_at: datetime)
2.2.0 :005 > Propimage.last
Propimage Load (0.7ms) SELECT "propimages".* FROM "propimages" ORDER BY "propimages"."id" DESC LIMIT 1
=> #<Propimage id: 37, property_id: 5, image: nil, created_at: "2015-07-15 20:06:55", updated_at: "2015-07-15 20:22:30">
2.2.0 :006 > Propimage.last.update_attributes(image: 'Picture.jpg', property_id: 3)
Propimage Load (0.3ms) SELECT "propimages".* FROM "propimages" ORDER BY "propimages"."id" DESC LIMIT 1
(0.1ms) begin transaction
Propimage Load (0.2ms) SELECT "propimages".* FROM "propimages" WHERE "propimages"."id" = ? LIMIT 1 [["id", 37]]
SQL (0.5ms) UPDATE "propimages" SET "image" = ?, "property_id" = ?, "updated_at" = ? WHERE "propimages"."id" = ? [["image", nil], ["property_id", 3], ["updated_at", "2015-07-15 20:24:06.501042"], ["id", 37]]
(1.1ms) commit transaction
=> true
2.2.0 :007 > Propimage.last
Propimage Load (0.2ms) SELECT "propimages".* FROM "propimages" ORDER BY "propimages"."id" DESC LIMIT 1
=> #<Propimage id: 37, property_id: 3, image: nil, created_at: "2015-07-15 20:06:55", updated_at: "2015-07-15 20:24:06">
2.2.0 :008 >
And here I try to create a new entry
2.2.0 :009 > Propimage.new(image: 'Picture.jpg', property_id: 5).save
(0.2ms) begin transaction
SQL (1.0ms) INSERT INTO "propimages" ("image", "property_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["image", nil], ["property_id", 5], ["created_at", "2015-07-15 20:30:17.797735"], ["updated_at", "2015-07-15 20:30:17.797735"]]
(1.7ms) commit transaction
=> true
My next step is to recreate this table in the database. Any help is greatly appreciated.
class CreatePropimages < ActiveRecord::Migration
def change
create_table :propimages do |t|
t.references :property, index: true, foreign_key: true
t.string :image
t.timestamps null: false
end
end
end
propimage.rb
class Propimage < ActiveRecord::Base
belongs_to :property
mount_uploader :image, ImageUploader
end

undefined method with has_many association

I have 2 models with a one-to-many association: User and Recipe. the User class has_many :recipes while the Recipe class belongs_to :user. I've already run the migration, reloaded the rails console, and checked to make sure that user_id is a column in the recipes table. Still, I get an undefined method error when I try to append a recipe to a user:
2.0.0-p598 :047 > user.recipes << Recipe.first
NoMethodError: undefined method `recipes' for #<User:0x00000004326fa0>
here is the migration code (I've already run rake db:migrate):
class AddUserIdToRecipes < ActiveRecord::Migration
def change
add_column :recipes, :user_id, :integer
end
end
Here is the User model code:
class User < ActiveRecord::Base
has_one :profile
has_many :recipes
end
Here is the Recipe model code:
class Recipe < ActiveRecord::Base
validates_presence_of :title, :body
belongs_to :user
def long_title
"#{title} - #{published_at}"
end
end
Why does recipes still show up as an undefined method?
Try this on your console:
irb(main):007:0> user = User.new first_name: 'John', last_name: 'Doe'
=> #<User id: nil, first_name: "John", last_name: "Doe", created_at: nil, updated_at: nil>
irb(main):008:0> user.save
(0.1ms) begin transaction
SQL (0.6ms) INSERT INTO "users" ("created_at", "first_name", "last_name", "updated_at") VALUES (?, ?, ?, ?) [["created_at", "2015-01-19 21:14:33.489371"], ["first_name", "John"], ["last_name", "Doe"], ["updated_at", "2015-01-19 21:14:33.489371"]]
(0.6ms) commit transaction
=> true
irb(main):009:0> r = Recipe.new name: 'oooohh awesome', description: 'my description goes here'
=> #<Recipe id: nil, name: "oooohh awesome", description: "my description goes here", created_at: nil, updated_at: nil, user_id: nil>
irb(main):010:0> r.save
(0.1ms) begin transaction
SQL (0.2ms) INSERT INTO "recipes" ("created_at", "description", "name", "updated_at") VALUES (?, ?, ?, ?) [["created_at", "2015-01-19 21:15:16.548090"], ["description", "my description goes here"], ["name", "oooohh awesome"], ["updated_at", "2015-01-19 21:15:16.548090"]]
(1.2ms) commit transaction
=> true
irb(main):011:0> user.recipes << Recipe.first
Recipe Load (0.2ms) SELECT "recipes".* FROM "recipes" ORDER BY "recipes"."id" ASC LIMIT 1
(0.0ms) begin transaction
SQL (0.2ms) UPDATE "recipes" SET "updated_at" = ?, "user_id" = ? WHERE "recipes"."id" = 1 [["updated_at", "2015-01-19 21:15:49.181586"], ["user_id", 1]]
(1.3ms) commit transaction
Recipe Load (0.2ms) SELECT "recipes".* FROM "recipes" WHERE "recipes"."user_id" = ? [["user_id", 1]]
=> #<ActiveRecord::Associations::CollectionProxy [#<Recipe id: 1, name: "oooohh awesome", description: "sper long deskdk", created_at: "2015-01-19 21:10:24", updated_at: "2015-01-19 21:15:49", user_id: 1>]>
irb(main):012:0> user.save
(0.1ms) begin transaction
(0.0ms) commit transaction
=> true
irb(main):013:0> user.recipes
=> #<ActiveRecord::Associations::CollectionProxy [#<Recipe id: 1, name: "oooohh awesome", description: "sper long deskdk", created_at: "2015-01-19 21:10:24", updated_at: "2015-01-19 21:15:49", user_id: 1>]>
irb(main):014:0> user.recipes.first
=> #<Recipe id: 1, name: "oooohh awesome", description: "sper long deskdk", created_at: "2015-01-19 21:10:24", updated_at: "2015-01-19 21:15:49", user_id: 1>
irb(main):015:0>
you can see that Recipe.first has been inserted into user.recipes and its saved.
I made two models similar to yours, and have exactly the same setup as you. You can follow code above to write your controllers.

has_one, dependent: destroy not working

I am using Devise for my user authentication and would like to destroy an associated profile along with the user.
My failing spec looks like this:
it "should destroy associated profile" do
profile = #user.profile
#user.destroy
expect(profile).to be_nil
end
And
In my user model:
has_one :profile, dependent: :destroy
In my profile model:
belongs_to :user
In the console, I can reproduce the issue like this:
2.0.0p247 :001 > #user = FactoryGirl.create(:user)
(1.5ms) BEGIN
User Exists (2.9ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = 'person946979#example.com' LIMIT 1
User Exists (1.7ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('person946979#example.com') LIMIT 1
SQL (15.7ms) INSERT INTO "users" ("created_at", "email", "encrypted_password", "name", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["created_at", Fri, 16 Aug 2013 01:21:12 UTC +00:00], ["email", "person946979#example.com"], ["encrypted_password", "$2a$10$0704XOlw.6ZE4HEfDhaIeuwnEbbJZvZda3Jwr052aLS5z3G77Dgja"], ["name", "Example User"], ["updated_at", Fri, 16 Aug 2013 01:21:12 UTC +00:00]]
SQL (3.8ms) INSERT INTO "profiles" ("created_at", "updated_at", "user_id") VALUES ($1, $2, $3) RETURNING "id" [["created_at", Fri, 16 Aug 2013 01:21:12 UTC +00:00], ["updated_at", Fri, 16 Aug 2013 01:21:12 UTC +00:00], ["user_id", 25]]
Profile Load (3.4ms) SELECT "profiles".* FROM "profiles" WHERE "profiles"."user_id" = $1 ORDER BY "profiles"."id" ASC LIMIT 1 [["user_id", 25]]
(2.2ms) COMMIT
=> #<User id: 25, email: "person946979#example.com", encrypted_password: "$2a$10$0704XOlw.6ZE4HEfDhaIeuwnEbbJZvZda3Jwr052aLS5...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: "2013-08-16 01:21:12", updated_at: "2013-08-16 01:21:12", name: "Example User">
2.0.0p247 :002 > #user.destroy
(1.0ms) BEGIN
SQL (2.5ms) DELETE FROM "profiles" WHERE "profiles"."id" = $1 [["id", 4]]
SQL (5.4ms) DELETE FROM "users" WHERE "users"."id" = $1 [["id", 25]]
(2.0ms) COMMIT
=> #<User id: 25, email: "person946979#example.com", encrypted_password: "$2a$10$0704XOlw.6ZE4HEfDhaIeuwnEbbJZvZda3Jwr052aLS5...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: "2013-08-16 01:21:12", updated_at: "2013-08-16 01:21:12", name: "Example User">
Interestingly, the user appears to actually have been deleted.
2.0.0p247 :003 > #user.reload.destroy
User Load (2.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 25]]
ActiveRecord::RecordNotFound: Couldn't find User with id=25
What is going on here?
Your model seems fine. Try something like this:
it "should destroy associated profile" do
profile = #user.profile
#user.destroy
expect(Profile.find(profile.id)).to be_nil
end
Like Heungju said, while the database row that corresponds to profile is being destroyed, the variable itself isn't.
How about this?
it "should destroy associated profile" do
profile = #user.profile
#user.destroy
expect(#user.profile).to be_nil
end
After #user.destroy, the 'profile', variable that expected to be nil, was not changed. I think...
Rewriting my spec like this does what I need:
it "should destroy associated profile" do
expect {
#user.destroy
}.to change(Profile, :count).by(-1)
end

Resources