CamelCamelCamel
CamelCamelCamel

Reputation: 5200

Rails :: Why the attribute change doesn't persist?

Controller:

@events = Event.all
@events.each { |e| e.user_subscribed = "someuser" }
@events.each { |e| puts "error" + e.user_subscribed }

I have attr_accessor :user_subscribed. but the error is can't convert nil into String as e.user_subscribed evaluates to nil.

I'm using mongoid on the backend.

edit: this works, but it just copies the whole array.

@events = @events.map do |e|
  e.user_subscribed = "faaa"
  e
end

Upvotes: 0

Views: 93

Answers (2)

cgr
cgr

Reputation: 1121

edited based on OP comments.

sounds like it might be better to just output Event.user_subscribed(current_user) directly in the view...but if you wanted to load up all that data before hand you could do:

@array_of_subscription_results = @Events.map{|e| e.user_subscribed(current_user,some,other,var,required) }

As long as user_subscribed returns the values you are interested in, thats what map will load into @array_of_subscription_results

Upvotes: 1

Alex Peattie
Alex Peattie

Reputation: 27697

If you're not saving the @events to the database, user_subscribed won't persist, unless you keep it in memory:

@events_with_subscription = @events.map { |e| e.user_subscribed = "someuser"; return e }

Upvotes: 2

Related Questions