Sunday, 18 August 2013

Accessing properties of another object in Rails

Accessing properties of another object in Rails

Currently I am working on CRM app as a demo project. Currently I'm having
difficulty wrapping my head around how to access properties of one object
from another. I'm trying to do something along the lines of:
<%= note.client.first_name %>
In this case, I have a note for each client and the appropriate
association set up between the two. The models look like so:
class Note < ActiveRecord::Base
belongs_to :client
belongs_to :user
end
class Client < ActiveRecord::Base
has_many :users
has_many :notes
end
And the databases look like so:
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :first_name
t.string :last_name
t.string :designation
t.string :phone
t.string :email
t.string :password_digest
t.timestamps
end
end
end
Is there any easy way of accessing the :first_name property of the client
id associated with the note?

No comments:

Post a Comment