Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To access joined columns when using Ruby with active record, you can use the dot notation to chain the table names together, followed by the column name. For example, if you have a users table joined with a posts table on the user_id foreign key, you can access the title column of the posts table for a user like this:

user = User.joins(:posts).where(id: 1).first
post_title = user.posts.first.title

In this code, we join the posts table with the users table using .joins(:posts) and then filter for a user with an id of 1 using .where(id: 1). We then use .first to get the first user matching that condition. Finally, we can access the title of the first post for that user using user.posts.first.title.