1

I have an array of a user's favourite_guidelines. In my console it looks like this:

fg=User.first.favourite_guidelines
  User Load (73.9ms)  SELECT "users".* FROM "users" LIMIT 1
  FavouriteGuideline Load (0.2ms)  SELECT "favourite_guidelines".* FROM "favourite_guidelines" WHERE "favourite_guidelines"."user_id" = 11
 => [#<FavouriteGuideline id: 16, guideline_id: 24, user_id: 11, created_at: "2013-02-17 01:03:19", updated_at: "2013-02-17 01:03:19">, #<FavouriteGuideline id: 18, guideline_id: 23, user_id: 11, created_at: "2013-02-17 11:40:27", updated_at: "2013-02-17 11:40:27">] 

So it contains two guidelines. I would like to be able to delete a value of a certain guideline_id so that the user's favourite guidelines would have that favourite removed. At the moment I have:

User.first,favourite_guidelines.delete_if{|favourite| favourite["guideline_id"] == 24}

That gives me:

.9.3p194 :066 > User.first.favourite_guidelines.delete_if{|favourite| favourite["guideline_id"] == 24}
  User Load (0.4ms)  SELECT "users".* FROM "users" LIMIT 1
  FavouriteGuideline Load (0.3ms)  SELECT "favourite_guidelines".* FROM "favourite_guidelines" WHERE "favourite_guidelines"."user_id" = 11
 => [#<FavouriteGuideline id: 18, guideline_id: 23, user_id: 11, created_at: "2013-02-17 11:40:27", updated_at: "2013-02-17 11:40:27">]'

So it looks like it is deleteing it but it's obviously not saving because then if I do:

    1.9.3p194 :067 > User.first.favourite_guidelines
  User Load (0.4ms)  SELECT "users".* FROM "users" LIMIT 1
  FavouriteGuideline Load (0.4ms)  SELECT "favourite_guidelines".* FROM "favourite_guidelines" WHERE "favourite_guidelines"."user_id" = 11
 => [#<FavouriteGuideline id: 16, guideline_id: 24, user_id: 11, created_at: "2013-02-17 01:03:19", updated_at: "2013-02-17 01:03:19">, #<FavouriteGuideline id: 18, guideline_id: 23, user_id: 11, created_at: "2013-02-17 11:40:27", updated_at: "2013-02-17 11:40:27">]

What am I doing wrong?

1
  • you are deleting from array not from table.. Commented Feb 17, 2013 at 11:56

2 Answers 2

1

Try this:

   User.first.favourite_guidelines.destroy( :guideline_id => 24)

Or

  User.first.favourite_guidelines.where( :guideline_id => 24).destroy_all
Sign up to request clarification or add additional context in comments.

Comments

1

delete_if works on Array of returned records but not on database. Try:

User.first.favourite_guidelines.where(guideline_id: 24).destroy_all

PS. Also you could look here

4 Comments

yes - that's it! Is there a way to pass something other than the id though (i.e. I want to pass the guideline_id but this takes the favourite_guideline_id)
Yes. See the link added. You can destroy_all(some_field: some_value)
Yes!!! I love you. Thank you. User.first.favourite_guidelines.where(:guideline_id => 24).destroy_all
This is wrong. It will delete favourite_guideline with id 24 not with guide_line_id 24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.