Home > database >  Ruby on Rails Active record object
Ruby on Rails Active record object

Time:09-17

I have a User model and having has_many associations with Note model. Getting the records in an array format. I have to show content(column of note model) only. So, Fetching data using:

 @user.notes.pluck(:content).

It is giving like this.

["Testing Notes123", "Testing Notes12345"].

In UI I want to display like this:

  Testing Notes123
  Testing Notes12345

Could someone please help me to fix this.

CodePudding user response:

By the "UI" do you mean a web page? That would involve a controller and a view. If you're just talking about the console, then this should do it:

notes = @user.notes.pluck(:content)
notes.each do |note|
  puts note
end

Or:

notes = @user.notes.pluck(:content)
puts notes.join("\n")
  • Related