Re: [Rails] has_many_through new association for exisitng records

2018-02-04 Thread fugee ohu


On Sunday, February 4, 2018 at 6:25:46 PM UTC-5, Walter Lee Davis wrote:
>
> Even more interesting (to me, anyway) is what happens if you create (but 
> don't save) any of these objects: 
>
> 2.4.2 :006 > p = Person.new name: 'Walter' 
>  => # 
> 2.4.2 :007 > c = Picture.new file: 'some file' 
>  => # nil> 
> 2.4.2 :008 > p.pictures << c 
>  => # file: "some file", created_at: nil, updated_at: nil>]> 
> 2.4.2 :009 > p.save 
>(1.9ms)  begin transaction 
>   SQL (23.1ms)  INSERT INTO "people" ("name", "created_at", "updated_at") 
> VALUES (?, ?, ?)  [["name", "Walter"], ["created_at", "2018-02-04 
> 23:23:15.342294"], ["updated_at", "2018-02-04 23:23:15.342294"]] 
>   SQL (0.3ms)  INSERT INTO "pictures" ("file", "created_at", "updated_at") 
> VALUES (?, ?, ?)  [["file", "some file"], ["created_at", "2018-02-04 
> 23:23:15.374862"], ["updated_at", "2018-02-04 23:23:15.374862"]] 
>   SQL (0.4ms)  INSERT INTO "person_pictures" ("person_id", "picture_id", 
> "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["person_id", 2], 
> ["picture_id", 2], ["created_at", "2018-02-04 23:23:15.376805"], 
> ["updated_at", "2018-02-04 23:23:15.376805"]] 
>(1.3ms)  commit transaction 
>  => true 
> 2.4.2 :010 > 
>
> When you save one of them, all three are saved in order to preserve the 
> entire set of relationships. 
>
> Walter 
>
> > On Feb 4, 2018, at 4:54 PM, Walter Lee Davis  > wrote: 
> > 
> > I can't duplicate this finding here. Here's the console log: 
> > 
> > 2.4.2 :001 > p = Person.new name: 'Walter' 
> > => # 
> > 2.4.2 :002 > p.save 
> >   (0.2ms)  begin transaction 
> >  SQL (2.8ms)  INSERT INTO "people" ("name", "created_at", "updated_at") 
> VALUES (?, ?, ?)  [["name", "Walter"], ["created_at", "2018-02-04 
> 21:47:31.000973"], ["updated_at", "2018-02-04 21:47:31.000973"]] 
> >   (1.3ms)  commit transaction 
> > => true 
> > 2.4.2 :003 > c = Picture.new file: 'some file' 
> > => # nil> 
> > 2.4.2 :004 > c.save 
> >   (0.2ms)  begin transaction 
> >  SQL (2.2ms)  INSERT INTO "pictures" ("file", "created_at", 
> "updated_at") VALUES (?, ?, ?)  [["file", "some file"], ["created_at", 
> "2018-02-04 21:47:54.717609"], ["updated_at", "2018-02-04 
> 21:47:54.717609"]] 
> >   (1.4ms)  commit transaction 
> > => true 
> > 2.4.2 :005 > p.pictures << c 
> >   (0.1ms)  begin transaction 
> >  SQL (0.6ms)  INSERT INTO "person_pictures" ("person_id", "picture_id", 
> "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["person_id", 1], 
> ["picture_id", 1], ["created_at", "2018-02-04 21:47:58.847298"], 
> ["updated_at", "2018-02-04 21:47:58.847298"]] 
> >   (1.1ms)  commit transaction 
> >  Picture Load (0.4ms)  SELECT  "pictures".* FROM "pictures" INNER JOIN 
> "person_pictures" ON "pictures"."id" = "person_pictures"."picture_id" WHERE 
> "person_pictures"."person_id" = ? LIMIT ?  [["person_id", 1], ["LIMIT", 
> 11]] 
> > => # "some file", created_at: "2018-02-04 21:47:54", updated_at: "2018-02-04 
> 21:47:54">]> 
> > 2.4.2 :006 > 
> > 
> > Here's the models: 
> > 
> > class Picture < ApplicationRecord 
> >  has_many :person_pictures 
> >  has_many :people, through: :person_pictures 
> > end 
> > 
> > class Person < ApplicationRecord 
> >  has_many :person_pictures 
> >  has_many :pictures, through: :person_pictures 
> > end 
> > 
> > class PersonPicture < ApplicationRecord 
> >  belongs_to :person 
> >  belongs_to :picture 
> > end 
> > 
> > Whatever is happening on your app is not clear, but you can see that 
> after you save the person and the picture, when you add that saved picture 
> to the saved person's 'pictures' collection, the only record that gets 
> created is a person_picture. Now if either the person or the picture was in 
> the "new" state, that is to say, not saved yet, then I could imagine that 
> it would be saved by ActiveRecord first in order to allow the 
> person_picture record to be saved. Both IDs have to be known before the 
> join object can be saved. 
> > 
> > Walter 
> > 
> >> On Feb 4, 2018, at 2:30 PM, fugee ohu  
> wrote: 
> >> 
> >> In a has_many_through association where both records exist how do I 
> create a new association 
> >> In this case Person has_many_pictures through person_picture and I'm 
> trying to add an existing picture to an existing person like this: 
> >> @person.pictures << @picture 
> >> But this creates a new picture instead of adding the association 
> >> 
> >> 
> >> -- 
> >> You received this message because you are subscribed to the Google 
> Groups "Ruby on Rails: Talk" group. 
> >> To unsubscribe from this group and stop receiving emails from it, send 
> an email to rubyonrails-ta...@googlegroups.com . 
> >> To post to this group, send email to rubyonra...@googlegroups.com 
> . 
> >> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/rubyonrails-talk/741e9a16-483f-4fc6-a2e7-77706b9b250c%40googlegroups.com.
>  
>
> >> For more options, visit https://groups.google.com/d/optout. 
> > 
> > 

Re: [Rails] has_many_through new association for exisitng records

2018-02-04 Thread fugee ohu


On Sunday, February 4, 2018 at 6:25:46 PM UTC-5, Walter Lee Davis wrote:
>
> Even more interesting (to me, anyway) is what happens if you create (but 
> don't save) any of these objects: 
>
> 2.4.2 :006 > p = Person.new name: 'Walter' 
>  => # 
> 2.4.2 :007 > c = Picture.new file: 'some file' 
>  => # nil> 
> 2.4.2 :008 > p.pictures << c 
>  => # file: "some file", created_at: nil, updated_at: nil>]> 
> 2.4.2 :009 > p.save 
>(1.9ms)  begin transaction 
>   SQL (23.1ms)  INSERT INTO "people" ("name", "created_at", "updated_at") 
> VALUES (?, ?, ?)  [["name", "Walter"], ["created_at", "2018-02-04 
> 23:23:15.342294"], ["updated_at", "2018-02-04 23:23:15.342294"]] 
>   SQL (0.3ms)  INSERT INTO "pictures" ("file", "created_at", "updated_at") 
> VALUES (?, ?, ?)  [["file", "some file"], ["created_at", "2018-02-04 
> 23:23:15.374862"], ["updated_at", "2018-02-04 23:23:15.374862"]] 
>   SQL (0.4ms)  INSERT INTO "person_pictures" ("person_id", "picture_id", 
> "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["person_id", 2], 
> ["picture_id", 2], ["created_at", "2018-02-04 23:23:15.376805"], 
> ["updated_at", "2018-02-04 23:23:15.376805"]] 
>(1.3ms)  commit transaction 
>  => true 
> 2.4.2 :010 > 
>
> When you save one of them, all three are saved in order to preserve the 
> entire set of relationships. 
>
> Walter 
>
> > On Feb 4, 2018, at 4:54 PM, Walter Lee Davis  > wrote: 
> > 
> > I can't duplicate this finding here. Here's the console log: 
> > 
> > 2.4.2 :001 > p = Person.new name: 'Walter' 
> > => # 
> > 2.4.2 :002 > p.save 
> >   (0.2ms)  begin transaction 
> >  SQL (2.8ms)  INSERT INTO "people" ("name", "created_at", "updated_at") 
> VALUES (?, ?, ?)  [["name", "Walter"], ["created_at", "2018-02-04 
> 21:47:31.000973"], ["updated_at", "2018-02-04 21:47:31.000973"]] 
> >   (1.3ms)  commit transaction 
> > => true 
> > 2.4.2 :003 > c = Picture.new file: 'some file' 
> > => # nil> 
> > 2.4.2 :004 > c.save 
> >   (0.2ms)  begin transaction 
> >  SQL (2.2ms)  INSERT INTO "pictures" ("file", "created_at", 
> "updated_at") VALUES (?, ?, ?)  [["file", "some file"], ["created_at", 
> "2018-02-04 21:47:54.717609"], ["updated_at", "2018-02-04 
> 21:47:54.717609"]] 
> >   (1.4ms)  commit transaction 
> > => true 
> > 2.4.2 :005 > p.pictures << c 
> >   (0.1ms)  begin transaction 
> >  SQL (0.6ms)  INSERT INTO "person_pictures" ("person_id", "picture_id", 
> "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["person_id", 1], 
> ["picture_id", 1], ["created_at", "2018-02-04 21:47:58.847298"], 
> ["updated_at", "2018-02-04 21:47:58.847298"]] 
> >   (1.1ms)  commit transaction 
> >  Picture Load (0.4ms)  SELECT  "pictures".* FROM "pictures" INNER JOIN 
> "person_pictures" ON "pictures"."id" = "person_pictures"."picture_id" WHERE 
> "person_pictures"."person_id" = ? LIMIT ?  [["person_id", 1], ["LIMIT", 
> 11]] 
> > => # "some file", created_at: "2018-02-04 21:47:54", updated_at: "2018-02-04 
> 21:47:54">]> 
> > 2.4.2 :006 > 
> > 
> > Here's the models: 
> > 
> > class Picture < ApplicationRecord 
> >  has_many :person_pictures 
> >  has_many :people, through: :person_pictures 
> > end 
> > 
> > class Person < ApplicationRecord 
> >  has_many :person_pictures 
> >  has_many :pictures, through: :person_pictures 
> > end 
> > 
> > class PersonPicture < ApplicationRecord 
> >  belongs_to :person 
> >  belongs_to :picture 
> > end 
> > 
> > Whatever is happening on your app is not clear, but you can see that 
> after you save the person and the picture, when you add that saved picture 
> to the saved person's 'pictures' collection, the only record that gets 
> created is a person_picture. Now if either the person or the picture was in 
> the "new" state, that is to say, not saved yet, then I could imagine that 
> it would be saved by ActiveRecord first in order to allow the 
> person_picture record to be saved. Both IDs have to be known before the 
> join object can be saved. 
> > 
> > Walter 
> > 
> >> On Feb 4, 2018, at 2:30 PM, fugee ohu  
> wrote: 
> >> 
> >> In a has_many_through association where both records exist how do I 
> create a new association 
> >> In this case Person has_many_pictures through person_picture and I'm 
> trying to add an existing picture to an existing person like this: 
> >> @person.pictures << @picture 
> >> But this creates a new picture instead of adding the association 
> >> 
> >> 
> >> -- 
> >> You received this message because you are subscribed to the Google 
> Groups "Ruby on Rails: Talk" group. 
> >> To unsubscribe from this group and stop receiving emails from it, send 
> an email to rubyonrails-ta...@googlegroups.com . 
> >> To post to this group, send email to rubyonra...@googlegroups.com 
> . 
> >> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/rubyonrails-talk/741e9a16-483f-4fc6-a2e7-77706b9b250c%40googlegroups.com.
>  
>
> >> For more options, visit https://groups.google.com/d/optout. 
> > 
> > 

[Rails] module A class B vs class A::B

2018-02-04 Thread fugee ohu
Are those two the same thing? I'm overriding a gem model so I put it in a 
namespace folder under models but what about modifying the file? Should I 
leave the module statement the way it is or remove it and replace the class 
name with the same prefix Module::Model

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/f0953bd7-7cb5-40de-8d87-57b36c9fdcf8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[Rails] passing parameters for polymorphic associations namespaced table name gets converted to double colon

2018-02-04 Thread fugee ohu
I copied the model from the blogit gem to my app and namespaced it blogit 
in order to override the gem now BlogitPost, when passed in parameters gets 
converted to Blogit::Post and controllers are looking for 
params[:imageable_type]=='Blogit' but it's not matching because the value 
of the parameter is actually 'Blogit::Post'

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/cf8bbbac-7e16-4904-be70-6991d1fe3064%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Rails] has_many_through new association for exisitng records

2018-02-04 Thread fugee ohu


On Sunday, February 4, 2018 at 4:55:08 PM UTC-5, Walter Lee Davis wrote:
>
> I can't duplicate this finding here. Here's the console log: 
>
> 2.4.2 :001 > p = Person.new name: 'Walter' 
>  => # 
> 2.4.2 :002 > p.save 
>(0.2ms)  begin transaction 
>   SQL (2.8ms)  INSERT INTO "people" ("name", "created_at", "updated_at") 
> VALUES (?, ?, ?)  [["name", "Walter"], ["created_at", "2018-02-04 
> 21:47:31.000973"], ["updated_at", "2018-02-04 21:47:31.000973"]] 
>(1.3ms)  commit transaction 
>  => true 
> 2.4.2 :003 > c = Picture.new file: 'some file' 
>  => # nil> 
> 2.4.2 :004 > c.save 
>(0.2ms)  begin transaction 
>   SQL (2.2ms)  INSERT INTO "pictures" ("file", "created_at", "updated_at") 
> VALUES (?, ?, ?)  [["file", "some file"], ["created_at", "2018-02-04 
> 21:47:54.717609"], ["updated_at", "2018-02-04 21:47:54.717609"]] 
>(1.4ms)  commit transaction 
>  => true 
> 2.4.2 :005 > p.pictures << c 
>(0.1ms)  begin transaction 
>   SQL (0.6ms)  INSERT INTO "person_pictures" ("person_id", "picture_id", 
> "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["person_id", 1], 
> ["picture_id", 1], ["created_at", "2018-02-04 21:47:58.847298"], 
> ["updated_at", "2018-02-04 21:47:58.847298"]] 
>(1.1ms)  commit transaction 
>   Picture Load (0.4ms)  SELECT  "pictures".* FROM "pictures" INNER JOIN 
> "person_pictures" ON "pictures"."id" = "person_pictures"."picture_id" WHERE 
> "person_pictures"."person_id" = ? LIMIT ?  [["person_id", 1], ["LIMIT", 
> 11]] 
>  => # "some file", created_at: "2018-02-04 21:47:54", updated_at: "2018-02-04 
> 21:47:54">]> 
> 2.4.2 :006 > 
>
> Here's the models: 
>
> class Picture < ApplicationRecord 
>   has_many :person_pictures 
>   has_many :people, through: :person_pictures 
> end 
>
> class Person < ApplicationRecord 
>   has_many :person_pictures 
>   has_many :pictures, through: :person_pictures 
> end 
>
> class PersonPicture < ApplicationRecord 
>   belongs_to :person 
>   belongs_to :picture 
> end 
>
> Whatever is happening on your app is not clear, but you can see that after 
> you save the person and the picture, when you add that saved picture to the 
> saved person's 'pictures' collection, the only record that gets created is 
> a person_picture. Now if either the person or the picture was in the "new" 
> state, that is to say, not saved yet, then I could imagine that it would be 
> saved by ActiveRecord first in order to allow the person_picture record to 
> be saved. Both IDs have to be known before the join object can be saved. 
>
> Walter 
>
> > On Feb 4, 2018, at 2:30 PM, fugee ohu  
> wrote: 
> > 
> > In a has_many_through association where both records exist how do I 
> create a new association 
> > In this case Person has_many_pictures through person_picture and I'm 
> trying to add an existing picture to an existing person like this: 
> > @person.pictures << @picture 
> > But this creates a new picture instead of adding the association 
> > 
> > 
> > -- 
> > You received this message because you are subscribed to the Google 
> Groups "Ruby on Rails: Talk" group. 
> > To unsubscribe from this group and stop receiving emails from it, send 
> an email to rubyonrails-ta...@googlegroups.com . 
> > To post to this group, send email to rubyonra...@googlegroups.com 
> . 
> > To view this discussion on the web visit 
> https://groups.google.com/d/msgid/rubyonrails-talk/741e9a16-483f-4fc6-a2e7-77706b9b250c%40googlegroups.com.
>  
>
> > For more options, visit https://groups.google.com/d/optout. 
>
>
class PersonPicture < ApplicationRecord
 belongs_to :person
 belongs_to :picture
end

class Picture < ApplicationRecord
 has_many :person_pictures
 has_many :people, through: :person_picture

class Person < ApplicationRecord
 has_many :person_pictures
 has_many :pictures, through: :person_pictures

@pictures = Person.find(params[:person_id]).pictures.all causes the error 
ActiveRecord::HasManyThroughAssociationNotFoundError in 
PicturesController#index
Could not find the association :person_picture in model Person

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/c8cf1eaa-d9fb-4378-adfc-a017ab04211a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Rails] has_many_through new association for exisitng records

2018-02-04 Thread Walter Lee Davis
Even more interesting (to me, anyway) is what happens if you create (but don't 
save) any of these objects:

2.4.2 :006 > p = Person.new name: 'Walter'
 => # 
2.4.2 :007 > c = Picture.new file: 'some file'
 => # 
2.4.2 :008 > p.pictures << c
 => #]> 
2.4.2 :009 > p.save
   (1.9ms)  begin transaction
  SQL (23.1ms)  INSERT INTO "people" ("name", "created_at", "updated_at") 
VALUES (?, ?, ?)  [["name", "Walter"], ["created_at", "2018-02-04 
23:23:15.342294"], ["updated_at", "2018-02-04 23:23:15.342294"]]
  SQL (0.3ms)  INSERT INTO "pictures" ("file", "created_at", "updated_at") 
VALUES (?, ?, ?)  [["file", "some file"], ["created_at", "2018-02-04 
23:23:15.374862"], ["updated_at", "2018-02-04 23:23:15.374862"]]
  SQL (0.4ms)  INSERT INTO "person_pictures" ("person_id", "picture_id", 
"created_at", "updated_at") VALUES (?, ?, ?, ?)  [["person_id", 2], 
["picture_id", 2], ["created_at", "2018-02-04 23:23:15.376805"], ["updated_at", 
"2018-02-04 23:23:15.376805"]]
   (1.3ms)  commit transaction
 => true 
2.4.2 :010 > 

When you save one of them, all three are saved in order to preserve the entire 
set of relationships.

Walter

> On Feb 4, 2018, at 4:54 PM, Walter Lee Davis  wrote:
> 
> I can't duplicate this finding here. Here's the console log:
> 
> 2.4.2 :001 > p = Person.new name: 'Walter'
> => # 
> 2.4.2 :002 > p.save
>   (0.2ms)  begin transaction
>  SQL (2.8ms)  INSERT INTO "people" ("name", "created_at", "updated_at") 
> VALUES (?, ?, ?)  [["name", "Walter"], ["created_at", "2018-02-04 
> 21:47:31.000973"], ["updated_at", "2018-02-04 21:47:31.000973"]]
>   (1.3ms)  commit transaction
> => true 
> 2.4.2 :003 > c = Picture.new file: 'some file'
> => # 
> 2.4.2 :004 > c.save
>   (0.2ms)  begin transaction
>  SQL (2.2ms)  INSERT INTO "pictures" ("file", "created_at", "updated_at") 
> VALUES (?, ?, ?)  [["file", "some file"], ["created_at", "2018-02-04 
> 21:47:54.717609"], ["updated_at", "2018-02-04 21:47:54.717609"]]
>   (1.4ms)  commit transaction
> => true 
> 2.4.2 :005 > p.pictures << c
>   (0.1ms)  begin transaction
>  SQL (0.6ms)  INSERT INTO "person_pictures" ("person_id", "picture_id", 
> "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["person_id", 1], 
> ["picture_id", 1], ["created_at", "2018-02-04 21:47:58.847298"], 
> ["updated_at", "2018-02-04 21:47:58.847298"]]
>   (1.1ms)  commit transaction
>  Picture Load (0.4ms)  SELECT  "pictures".* FROM "pictures" INNER JOIN 
> "person_pictures" ON "pictures"."id" = "person_pictures"."picture_id" WHERE 
> "person_pictures"."person_id" = ? LIMIT ?  [["person_id", 1], ["LIMIT", 11]]
> => # "some file", created_at: "2018-02-04 21:47:54", updated_at: "2018-02-04 
> 21:47:54">]> 
> 2.4.2 :006 > 
> 
> Here's the models:
> 
> class Picture < ApplicationRecord
>  has_many :person_pictures
>  has_many :people, through: :person_pictures
> end
> 
> class Person < ApplicationRecord
>  has_many :person_pictures
>  has_many :pictures, through: :person_pictures
> end
> 
> class PersonPicture < ApplicationRecord
>  belongs_to :person
>  belongs_to :picture
> end
> 
> Whatever is happening on your app is not clear, but you can see that after 
> you save the person and the picture, when you add that saved picture to the 
> saved person's 'pictures' collection, the only record that gets created is a 
> person_picture. Now if either the person or the picture was in the "new" 
> state, that is to say, not saved yet, then I could imagine that it would be 
> saved by ActiveRecord first in order to allow the person_picture record to be 
> saved. Both IDs have to be known before the join object can be saved.
> 
> Walter
> 
>> On Feb 4, 2018, at 2:30 PM, fugee ohu  wrote:
>> 
>> In a has_many_through association where both records exist how do I create a 
>> new association
>> In this case Person has_many_pictures through person_picture and I'm trying 
>> to add an existing picture to an existing person like this:
>> @person.pictures << @picture
>> But this creates a new picture instead of adding the association
>> 
>> 
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Ruby on Rails: Talk" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to rubyonrails-talk+unsubscr...@googlegroups.com.
>> To post to this group, send email to rubyonrails-talk@googlegroups.com.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/rubyonrails-talk/741e9a16-483f-4fc6-a2e7-77706b9b250c%40googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to rubyonrails-talk+unsubscr...@googlegroups.com.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To view this discussion on the web visit 
> 

Re: [Rails] has_many_through new association for exisitng records

2018-02-04 Thread Walter Lee Davis
I can't duplicate this finding here. Here's the console log:

2.4.2 :001 > p = Person.new name: 'Walter'
 => # 
2.4.2 :002 > p.save
   (0.2ms)  begin transaction
  SQL (2.8ms)  INSERT INTO "people" ("name", "created_at", "updated_at") VALUES 
(?, ?, ?)  [["name", "Walter"], ["created_at", "2018-02-04 21:47:31.000973"], 
["updated_at", "2018-02-04 21:47:31.000973"]]
   (1.3ms)  commit transaction
 => true 
2.4.2 :003 > c = Picture.new file: 'some file'
 => # 
2.4.2 :004 > c.save
   (0.2ms)  begin transaction
  SQL (2.2ms)  INSERT INTO "pictures" ("file", "created_at", "updated_at") 
VALUES (?, ?, ?)  [["file", "some file"], ["created_at", "2018-02-04 
21:47:54.717609"], ["updated_at", "2018-02-04 21:47:54.717609"]]
   (1.4ms)  commit transaction
 => true 
2.4.2 :005 > p.pictures << c
   (0.1ms)  begin transaction
  SQL (0.6ms)  INSERT INTO "person_pictures" ("person_id", "picture_id", 
"created_at", "updated_at") VALUES (?, ?, ?, ?)  [["person_id", 1], 
["picture_id", 1], ["created_at", "2018-02-04 21:47:58.847298"], ["updated_at", 
"2018-02-04 21:47:58.847298"]]
   (1.1ms)  commit transaction
  Picture Load (0.4ms)  SELECT  "pictures".* FROM "pictures" INNER JOIN 
"person_pictures" ON "pictures"."id" = "person_pictures"."picture_id" WHERE 
"person_pictures"."person_id" = ? LIMIT ?  [["person_id", 1], ["LIMIT", 11]]
 => #]> 
2.4.2 :006 > 

Here's the models:

class Picture < ApplicationRecord
  has_many :person_pictures
  has_many :people, through: :person_pictures
end

class Person < ApplicationRecord
  has_many :person_pictures
  has_many :pictures, through: :person_pictures
end

class PersonPicture < ApplicationRecord
  belongs_to :person
  belongs_to :picture
end

Whatever is happening on your app is not clear, but you can see that after you 
save the person and the picture, when you add that saved picture to the saved 
person's 'pictures' collection, the only record that gets created is a 
person_picture. Now if either the person or the picture was in the "new" state, 
that is to say, not saved yet, then I could imagine that it would be saved by 
ActiveRecord first in order to allow the person_picture record to be saved. 
Both IDs have to be known before the join object can be saved.

Walter

> On Feb 4, 2018, at 2:30 PM, fugee ohu  wrote:
> 
> In a has_many_through association where both records exist how do I create a 
> new association
> In this case Person has_many_pictures through person_picture and I'm trying 
> to add an existing picture to an existing person like this:
> @person.pictures << @picture
> But this creates a new picture instead of adding the association
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to rubyonrails-talk+unsubscr...@googlegroups.com.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/rubyonrails-talk/741e9a16-483f-4fc6-a2e7-77706b9b250c%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/EDF36112-47D8-4749-B974-F55A2C57DBA6%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.


[Rails] has_many_through new association for exisitng records

2018-02-04 Thread fugee ohu
In a has_many_through association where both records exist how do I create 
a new association
In this case Person has_many_pictures through person_picture and I'm trying 
to add an existing picture to an existing person like this:
@person.pictures << @picture
But this creates a new picture instead of adding the association

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/741e9a16-483f-4fc6-a2e7-77706b9b250c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[Rails] [Article] Ultimate Guide to Ruby String Methods

2018-02-04 Thread Jesus Castello
Hello!
I wrote an article on Ruby string methods 

 
that I would like to share with you.

Read it here:

https://www.rubyguides.com/2018/01/ruby-string-methods 


Thank you :)

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/9b47bdc9-6d5e-4d6f-ac72-107eeac21853%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Rails] Re: reciprocating relationships

2018-02-04 Thread fugee ohu


On Saturday, February 3, 2018 at 5:49:13 PM UTC-5, Walter Lee Davis wrote:
>
>
> > On Feb 3, 2018, at 3:56 PM, fugee ohu  
> wrote: 
> > 
> > 
> > 
> > On Friday, January 12, 2018 at 7:06:12 PM UTC-5, fugee ohu wrote: 
> > Let's say I have a table of products and a table of accessories Each 
> accessory can have many products that it's compatible with while each 
> product has many accessories available for it It In the problem I'm trying 
> to solve accessories cannot belong to products and products cannot belong 
> to accessories So, unless I was to create a third table with fields for 
> product_id and accessory_id, how could I allow each to have many of the 
> other Do I have to create the third table? Thanks in advance 
> > 
> > @person.pictures << @picture tries to, create a new picture, not add it 
> to the person_picture association table 
>
> Where does @picture come from? Did you build it in memory, or load it from 
> the database? Rather than adding an object you created to an association, 
> try building the associated object from the beginning, like this: 
>
> @picture = @person.pictures.build(picture_attributes) 
>
> Now that picture is automatically in the @person.pictures association, and 
> has whatever other attributes you assigned to it. 
>
> When you save the picture, the corresponding person_picture instance will 
> be persisted as well. The picture has to be created and saved before the 
> person_picture record can be, otherwise where would the 
> person_picture.picture_id value come from? 
>
> Walter


to do that i added accepts_nested_attributes_for :picture it didn't create 
the  association

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/ec2c1388-9392-4a5e-952b-5f27f6c7042c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.