1

i got 2 tables connected with each other.

device and push information are my models.

class Device < ActiveRecord::Base

    has_one :pushinformation
end


class Pushinformation < ActiveRecord::Base

    belongs_to :device
end

these are my 2 model files and their relationships.

and these are my migration files

class CreateDevices < ActiveRecord::Migration
  def change
    create_table :devices do |t|
      #t.integer :id
      t.string "token" 
      t.string "carrier"
      t.string "segment"
      #t.datetime :created_at
      #t.datetime :updated_at

      t.timestamps
    end
  end
end



class CreatePushinformations < ActiveRecord::Migration
  def change
    create_table :pushinformations do |t|

        t.integer "device_id"

        #t.string "token"
        t.string "first_name"
        t.string "last_name"
        t.string "nickname"

      t.timestamps
    end
  end
end

now the thing is , i was able to create a relationship successfully in rails console by saying

device.pushinformation=push

and they were associated.

How can i make this process automated, like when i add one device- it will have a push information table filled aswell,

i thought about having the same attribute and relating them might be the solution. In this case its token and its completely unique.

How can i create this relationships? I need to be able to know which device has what kind of first_name

i m a beginner in ruby and this is a newbie question sorry guys :)

databaseGuy
  • 37
  • 1
  • 8

3 Answers3

1

I am not sure I understand completely what you ask but my guess is that you could use a callback on create

class Pushinformation < ActiveRecord::Base
  belongs_to :device

  after_create :create_push_notification

  private

  def create_push_notification
    ...
  end
end

check the docs

http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html

xlembouras
  • 8,215
  • 4
  • 33
  • 42
0

Add a create method to your Devise class. Something like:

def self.create(token, carrier, segment)
 device = Device.new(:token => token, :carrier => carrier, :segment => segment)
 pi = Pushinformation.create(device.id,..) # other params 
end
shivam
  • 16,048
  • 3
  • 56
  • 71
0

xlembouras's answer is right (to a degree), but as you're new, let me explain it for you:

--

Associations

ActiveRecord associations are nothing magical, they're just a way to associate two "objects" using a relational database setup.

ActiveRecord is an ORM -- "object relationship mapper" -- which basically means it just provides a level of abstraction for your ActiveRecord objects to associate with each other

Herein lies the crux of the matter - you need to apprciate how and why your associations will work, and more importantly, how to populate them correctly:

--

Models

Firstly, you need to appreciate the object-orientated nature of Ruby (& by virtue of running on Ruby, Rails). This is where the Models of Rails play such a vital role -- they allow you to build & manage objects from your database

The ActiveRecord associations give you the ability to manage the associations those objects have - maning if you build one, you should be able to build the other:

#app/models/device.rb
Class Device < ActiveRecord::Base
   has_one :push_information
   before_create :build_push_information #-> creates associative object before creation
end

#app/models/push_information.rb
Class PushInformation < ActiveRecord::Base
   belongs_to :device
end

You need to consider the importance of what I've written above.

What you need is to create a push_information object with the same foreign_key as the device object, which can be achieved by using the build method

This will essentially create a "blank" version of your associative object, saving it with the correct foreign key etc

--

Nesting

Further to this, you have to appreciate the idea of "nesting", especially the method accepts_nested_attributes_for

This allows you to create associative / dependent objects based on your "parent" object:

#app/models/device.rb
Class Device < ActiveRecord::Base
   has_one :push_information
   accepts_nested_attributes_for :push_information
end

#app/models/push_informtion.rb
Class PushInformation < ActiveRecord::Base
   belongs_to :device
end

This gives you the ability to do the following:

#app/controllers/devices_controller.rb
Class DevicesController < ApplicationController
   def new
       @device = Device.new
       @device.build_push_information
   end

   def create
       @device = Device.new(device_params)
       @device.save
   end

   private

   def device_params
       params.require(:device).permit(:your, :device, :params, push_information_attributes: [:push, :information, :attributes])
   end
end

This gives you the ability to populate the devices#new form like so:

#app/views/devices/new.html.erb
<%= form_for @device do |f| %>
   <%= f.text_field :your_device_attributes %>
   <%= f.fields_for :push_information do |p| %>
       <%= p.text_field :your_field %>
   <% end %>
   <%= f.submit %>
<% end %>
Community
  • 1
  • 1
Richard Peck
  • 76,116
  • 9
  • 93
  • 147