Skip to main content
Ask A Question
You’re not receiving notifications from this thread.

Rails console hacks :)

Rails • Asked by Sascha M.

Hi there,

I need some help how to handle the rails console commands. I'm really stuck here..

I've recently added a new column (teams) to my Sales and Orders model. Both models containing the user_id from the User model.
The column team already exists in the User model. So I want to do this:

Fill the team names in the Sales and Orders model through the user_id from the User model and pull the team name from there.

Something like (pseudo sql): insert team into from User where user_id == Sale.user_id

I hope you get what I mean :)


I got it somehow.. Order.all.each do |tname| = User.where(:id => tname.user_id).pluck(:team).shift end

If I type this into the console the records get filled but not saved. What should I have to add to save the records?

Order.all.each do |tname| = User.where(:id => tname.user_id).pluck(:team).shift self.update_columns(team: end or Order.all.each do |tname| = User.where(:id => tname.user_id).pluck(:team).shift self.update_attributes(team: end are both not working.

>> NoMethodError: undefined method `update_attributes' for main:Object

Any ideas?

Hey Sascha, I'd write it like this:

Order.all.each do |tname| 
  tname.update_attributes(team: User.where(:id => tname.user_id).pluck(:team).shift)

The problem is when you use self it doesn't point to the tname object, causing that issue.

Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 30,005+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.